Essential Coding Patterns

Master these essential patterns to solve 80% of coding interview problems. Recognizing the right pattern is half the battle!

1

Two Pointers

O(n)

Use two pointers moving towards each other or in the same direction to solve array/string problems efficiently.

When to use: Sorted arrays, pairs with target sum, palindromes
Common Problems:
Two Sum in sorted array
Remove duplicates
Container with most water
Valid palindrome
Learn with Kid-Friendly Examples & Java Code →
2

Sliding Window

O(n)

Maintain a window of elements and slide it through the array to find optimal solutions.

When to use: Contiguous subarrays/substrings, max/min values in window
Common Problems:
Maximum sum subarray of size K
Longest substring without repeating characters
Minimum window substring
Fruits into baskets
Learn with Kid-Friendly Examples & Java Code →
3

Fast & Slow Pointers

O(n)

Use two pointers moving at different speeds to detect cycles or find middle elements.

When to use: Linked list cycles, finding middle element, palindrome check
Common Problems:
Detect cycle in linked list
Find middle of linked list
Happy number
Linked list cycle start
Learn with Kid-Friendly Examples & Java Code →
4

Merge Intervals

O(n log n)

Merge or process overlapping intervals by sorting and comparing boundaries.

When to use: Overlapping intervals, scheduling problems
Common Problems:
Merge intervals
Insert interval
Meeting rooms
Employee free time
Learn with Kid-Friendly Examples & Java Code →
5

Cyclic Sort

O(n)

Sort arrays containing numbers in a given range by placing each number at its correct index.

When to use: Arrays with numbers in range [1, n], finding missing/duplicate numbers
Common Problems:
Find missing number
Find all duplicates
Find corrupt pair
First missing positive
Learn with Kid-Friendly Examples & Java Code →
6

In-place Reversal of Linked List

O(n)

Reverse linked lists or portions of them by manipulating pointers in-place.

When to use: Linked list reversal problems
Common Problems:
Reverse linked list
Reverse sublist
Reverse every K-element sublist
Rotate list
Learn with Kid-Friendly Examples & Java Code →
7

Tree BFS (Breadth First Search)

O(n)

Traverse tree level by level using a queue to process nodes.

When to use: Level order traversal, finding depth, zigzag traversal
Common Problems:
Level order traversal
Zigzag traversal
Minimum depth
Connect level order siblings
Learn with Kid-Friendly Examples & Java Code →
8

Tree DFS (Depth First Search)

O(n)

Traverse tree paths from root to leaves using recursion or stack.

When to use: Path sum, all paths, tree diameter
Common Problems:
Path sum
All paths for sum
Sum of path numbers
Tree diameter
Learn with Kid-Friendly Examples & Java Code →
9

Two Heaps

O(log n) insert

Use two heaps (min and max) to efficiently find median or balance elements.

When to use: Finding median in stream, scheduling problems
Common Problems:
Find median from data stream
Sliding window median
IPO maximize capital
Next interval
Learn with Kid-Friendly Examples & Java Code →
10

Subsets

O(2^n)

Generate all possible combinations using BFS or backtracking.

When to use: Power set, combinations, permutations
Common Problems:
Subsets
Subsets with duplicates
Permutations
Letter case permutation
Learn with Kid-Friendly Examples & Java Code →
11

Modified Binary Search

O(log n)

Apply binary search on sorted or rotated arrays with modifications.

When to use: Sorted arrays, finding elements in rotated arrays
Common Problems:
Search in rotated array
Find peak element
Search in infinite array
Minimum difference element
Learn with Kid-Friendly Examples & Java Code →
12

Top K Elements

O(n log k)

Use heap or quickselect to find K largest/smallest elements efficiently.

When to use: Finding K largest/smallest, frequency problems
Common Problems:
Kth largest element
Top K frequent numbers
K closest points
Connect ropes with minimum cost
Learn with Kid-Friendly Examples & Java Code →
13

K-way Merge

O(n log k)

Merge K sorted arrays/lists using a heap to track smallest elements.

When to use: Merging sorted arrays/lists
Common Problems:
Merge K sorted lists
Kth smallest in sorted matrix
Smallest number range
K pairs with largest sums
Learn with Kid-Friendly Examples & Java Code →
14

Dynamic Programming (0/1 Knapsack)

O(n * capacity)

Choose or skip items to maximize/minimize value using memoization or tabulation.

When to use: Optimization problems with choices
Common Problems:
0/1 Knapsack
Subset sum
Equal subset sum partition
Target sum
Learn with Kid-Friendly Examples & Java Code →
15

Dynamic Programming (Unbounded Knapsack)

O(n * capacity)

Similar to 0/1 but items can be used unlimited times.

When to use: Unlimited use of items, coin change problems
Common Problems:
Unbounded knapsack
Coin change
Rod cutting
Minimum coin change
Learn with Kid-Friendly Examples & Java Code →
16

Dynamic Programming (LCS/LIS)

O(n * m)

Find longest common subsequence or increasing subsequence using DP.

When to use: Subsequence problems, string matching
Common Problems:
Longest common subsequence
Longest increasing subsequence
Edit distance
Distinct subsequences
Learn with Kid-Friendly Examples & Java Code →
17

Topological Sort

O(V + E)

Order graph nodes where dependencies come before dependents.

When to use: Directed acyclic graphs, ordering with dependencies
Common Problems:
Course schedule
Task scheduling
Alien dictionary
Sequence reconstruction
Learn with Kid-Friendly Examples & Java Code →
18

Monotonic Stack

O(n)

Maintain stack elements in monotonic order to find next greater/smaller elements.

When to use: Next greater/smaller element problems
Common Problems:
Next greater element
Daily temperatures
Largest rectangle in histogram
Trapping rain water
Learn with Kid-Friendly Examples & Java Code →
19

Backtracking

Exponential

Explore all possibilities by building solutions incrementally and backtracking on failure.

When to use: Constraint satisfaction, generating all solutions
Common Problems:
N-Queens
Sudoku solver
Word search
Generate parentheses
Learn with Kid-Friendly Examples & Java Code →
20

Union Find (Disjoint Set)

O(α(n)) ≈ O(1)

Track connected components and efficiently merge sets.

When to use: Graph connectivity, cycle detection, MST
Common Problems:
Number of islands
Redundant connection
Accounts merge
Minimum spanning tree
Learn with Kid-Friendly Examples & Java Code →
21

Recursion

Varies (O(n) to O(2^n))

Break down problems into smaller subproblems by having functions call themselves with simpler inputs.

When to use: Tree/graph traversal, divide and conquer, mathematical problems
Common Problems:
Factorial calculation
Fibonacci sequence
Tree traversals
Generate all combinations
Learn with Kid-Friendly Examples & Java Code →

How to Master These Patterns

1.Learn one pattern at a time - understand the concept thoroughly
2.Solve 3-5 problems for each pattern to reinforce understanding
3.Practice identifying which pattern to use when you see a new problem
4.Review patterns regularly - space repetition is key to retention
5.Time yourself to build speed - aim to recognize patterns quickly