Interview Prep · Topic 2 of 5

LeetCode Patterns

100 XP

The problem with random grinding

Solving 300 problems doesn’t make you interview-ready if you solved them by looking at hints and moving on. Recognizing patterns — and knowing which pattern a problem belongs to within 60 seconds — is what actually matters in an interview.

There are roughly 10 patterns. Every coding interview question is a variation of one (or a combination of two) of them.


Pattern 1: Two Pointers

Signal the interviewer is testing: Can you eliminate the O(n²) nested loop?

Trigger words: sorted array, pair sum, palindrome, container with most water, remove duplicates in-place.

Template: Left pointer at start, right at end. Move based on a comparison. O(n) time, O(1) space.

Problems: Two Sum II (sorted), 3Sum, Container with Most Water, Valid Palindrome.


Pattern 2: Sliding Window

Signal: Can you process subarrays/substrings without recomputing from scratch?

Trigger words: contiguous subarray, substring, “longest/shortest with condition X”.

Template: Expand right, shrink left when condition violated. Track state in a hashmap/counter.

Problems: Longest Substring Without Repeating Characters, Minimum Window Substring, Maximum Sum Subarray of Size K.


Pattern 3: Fast & Slow Pointers

Signal: Can you detect cycles or find the middle without extra space?

Trigger words: linked list cycle, find middle, palindrome linked list.

Template: Slow moves 1 step, fast moves 2. They meet → cycle exists. When fast hits null → no cycle.

Problems: Linked List Cycle, Find the Duplicate Number, Happy Number.


Pattern 4: Merge Intervals

Signal: Can you handle overlapping ranges efficiently?

Trigger words: intervals, overlapping, schedule, meeting rooms.

Template: Sort by start time. Walk through, merge if next.start <= current.end.

Problems: Merge Intervals, Insert Interval, Meeting Rooms II (count overlaps with a min-heap).


Signal: Can you reduce search space by half on each step?

Trigger words: sorted array, rotated sorted, “minimum/maximum that satisfies condition”, search space is monotonic.

Template: left <= right, mid = left + (right - left) / 2. Move left = mid + 1 or right = mid - 1.

Advanced: Binary search on the answer space — not on the array itself.

Problems: Search in Rotated Sorted Array, Find Minimum in Rotated Array, Koko Eating Bananas, Capacity to Ship Packages.


Pattern 6: Tree DFS

Signal: Can you think recursively about subtrees?

Trigger words: binary tree, path, depth, diameter, validate, subtree.

Template:

function solve(node):
  if node is null: return base case
  left = solve(node.left)
  right = solve(node.right)
  return combine(node.val, left, right)

Problems: Maximum Depth, Path Sum, Lowest Common Ancestor, Diameter of Binary Tree, Validate BST.


Pattern 7: Tree BFS / Level Order

Signal: Can you process a tree level by level?

Trigger words: level order, minimum depth, right side view, cousins.

Template: Queue. On each iteration, drain the entire current level (for i in range(queue.size())) before adding children.

Problems: Binary Tree Level Order Traversal, Minimum Depth of Binary Tree, Binary Tree Right Side View.


Pattern 8: Graph DFS / BFS

Signal: Can you explore a connected structure and track visited state?

Trigger words: islands, connected components, shortest path (BFS), cycle detection, topological sort.

Templates:

  • DFS: recursion or explicit stack, visited set
  • BFS: queue, visited set, track distance for shortest path
  • Topological sort: Kahn’s algorithm (BFS with in-degree counts)

Problems: Number of Islands, Clone Graph, Course Schedule (cycle detection + topological sort), Word Ladder (BFS shortest path).


Pattern 9: Dynamic Programming

Signal: Can you break this into subproblems with overlapping structure?

Trigger words: count ways, maximum/minimum value, “can you achieve X”, subsequence, partition.

Template: Define state, define recurrence, handle base cases, iterate in the right order (or memoize top-down).

Sub-patterns:

  • 1D DP: Fibonacci, Climbing Stairs, House Robber
  • 2D DP: Unique Paths, Edit Distance, Longest Common Subsequence
  • Interval DP: Matrix Chain Multiplication
  • Knapsack: 0/1 Knapsack, Coin Change

Problems: Climbing Stairs, Coin Change, Longest Increasing Subsequence, Edit Distance, Word Break.


Pattern 10: Heap / Priority Queue

Signal: Can you maintain a sorted order efficiently under insertions?

Trigger words: K largest, K smallest, median of a stream, top K frequent, merge K sorted.

Template: Min-heap of size K for “K largest” (counterintuitive but correct). Two heaps (max + min) for median.

JavaScript note: No built-in heap. In interviews, implement with a sorted array for small K, or ask if you can assume a heap library. Alternatively, sort once and take top K: O(n log n) — fine for interviews.

Problems: Kth Largest Element, Top K Frequent Elements, Find Median from Data Stream, Merge K Sorted Lists.


The meta-skill: pattern recognition under pressure

When you see a new problem:

  1. Read once, don’t code. What data structure is involved? What’s the constraint?
  2. Identify the trigger words. Sorted array → binary search or two pointers. Tree → DFS or BFS. “K largest” → heap.
  3. State the pattern out loud. “This looks like a sliding window problem.” Interviewers reward this.
  4. Write the template first, then fill in the specifics.
  5. Complexity before you write the first line. If you can’t estimate O(n²) before coding, you don’t understand the approach.

You’re not being tested on whether you’ve seen this exact problem. You’re being tested on whether you can classify it and apply a known technique in real time.


How many problems do you need?

Minimum viable: 2–3 problems per pattern × 10 patterns = 20–30 problems, done deeply.

“Deeply” means: no hints, no looking at solutions mid-solve, full working code, explained time/space complexity, identified what pattern it is before you start.

After that, grind variations. But if you haven’t done the 20–30 deeply, adding more problems just creates false confidence.