InterviewGrind Get notified

InterviewGrind › Coding patterns

Coding Interview Patterns: Practice Questions

Two pointers, sliding windows, BFS, binary search on the answer and more: pick the right pattern for each problem, the skill that unlocks most coding interviews.

15 free questions from the app. Try each one before you open the answer.

1. StackEasy

Check whether a string like "{[()]}" has balanced brackets. Which data structure fits?

  1. Stack
  2. Queue
  3. Hash set
  4. Heap
Show answer

Answer: A. Stack

Clue: nesting, where the most recent opener must close first (last in, first out). Push openers; on a closer, the top of the stack must be its match. The stack must be empty at the end.

2. Prefix sumsEasy

An array never changes, but you'll answer a million "sum from i to j" queries. What do you precompute?

  1. Prefix sums: each query is prefix[j + 1] - prefix[i]
  2. Nothing; just loop over i..j for each query
  3. A heap of the values, built once
  4. A sorted copy of the array, for binary search
Show answer

Answer: A. Prefix sums: each query is prefix[j + 1] - prefix[i]

One O(n) pass makes every query O(1). The 2D version (summed-area table) answers rectangle sums in a grid.

3. IntervalsEasy

Merge all overlapping intervals in a list like [[1,3],[2,6],[8,10]]. Which approach fits best?

  1. Sort by start, then merge each into the last kept interval
  2. Union-find over every pair of overlapping intervals
  3. BFS from each interval to find everything it touches
  4. Dynamic programming over the intervals in input order
Show answer

Answer: A. Sort by start, then merge each into the last kept interval

Clue: intervals plus "overlap". After sorting by start, an interval overlaps the previous merged one only if its start ≤ that one's end. O(n log n) for the sort, then one pass.

4. Flood fillEasy

Count the islands of 1s in a grid of 0s and 1s (cells connect up, down, left, right). Which approach fits?

  1. Flood fill (DFS or BFS) from each unvisited land cell
  2. Sort the grid's cells by row and column
  3. Binary search each row for its first 1
  4. Prefix sums over each row of the grid
Show answer

Answer: A. Flood fill (DFS or BFS) from each unvisited land cell

Scan the grid; each time you hit unvisited land, count an island and flood-fill everything connected to it so you don't count it again. O(rows × cols). Union-find also works.

5. Two pointersEasy

Given a sorted array, find two numbers that add up to a target using O(1) extra space. Which pattern fits best?

  1. Hash map of values seen so far
  2. Two pointers from both ends
  3. Dynamic programming
  4. Depth-first search
Show answer

Answer: B. Two pointers from both ends

Clue: sorted input plus a pair condition. If the sum is too small, move the left pointer right; if too big, move the right pointer left. O(n) time, O(1) space.

6. Fast and slow pointersEasy

Find the middle node of a singly linked list in a single pass. Which approach fits?

  1. Fast and slow pointers
  2. Recursion to the end, then back up halfway
  3. Copy into an array first
  4. Binary search
Show answer

Answer: A. Fast and slow pointers

Move one pointer one step and the other two steps at a time. When the fast one reaches the end, the slow one is in the middle. O(n) time, O(1) space.

7. Recognizing patternsEasy

Myth or fact: when a problem says "sorted array", the intended solution is binary search.

  1. Myth
  2. Fact
Show answer

Answer: A. Myth

Myth. Sorted input is a strong hint, but it often points to two pointers (pair sums, removing duplicates, merging) rather than binary search. Ask what the sortedness lets you skip.

8. BacktrackingEasy

Generate every subset of a list of distinct numbers. Which approach fits best?

  1. Backtracking: include or skip each element
  2. Sliding window over every window size
  3. Greedy: add elements while the subset stays valid
  4. Binary search over the possible subset sizes
Show answer

Answer: A. Backtracking: include or skip each element

Clue: "all combinations / subsets / permutations". Each element is a choose-or-skip decision, giving 2ⁿ subsets. Iterating a bitmask from 0 to 2ⁿ - 1 is an equivalent alternative.

9. Sliding windowMedium

Find the length of the longest substring that contains at most K distinct characters. Which pattern fits best?

  1. Sliding window
  2. Binary search
  3. Backtracking
  4. Union-find
Show answer

Answer: A. Sliding window

Clue: "longest contiguous substring" with a constraint you can update incrementally. Grow the right edge, shrink the left edge while there are more than K distinct characters, and track the best width. O(n) time, O(K) space.

10. HeapMedium

Numbers arrive one at a time. After each one, report the Kth largest seen so far. Which pattern fits best?

  1. Sort everything after each insert
  2. Min-heap of size K
  3. Max-heap of all numbers
  4. Two pointers
Show answer

Answer: B. Min-heap of size K

Clue: "top K" over a stream. Keep only the K largest in a min-heap; its root is the Kth largest. Each insert is O(log K), and space is O(K).

11. Monotonic stackMedium

For each item in an array, find the next item to its right that is larger. Which pattern fits best?

  1. Nested loops, checking each later item
  2. Monotonic stack
  3. Binary search
  4. Prefix sums
Show answer

Answer: B. Monotonic stack

Clue: "next greater/smaller". Keep a stack of indices still waiting for an answer, in decreasing order of value. Each new item pops every smaller item and becomes their answer. Every index is pushed and popped once: O(n).

12. Union-findMedium

Given pairs of people who are friends, count the separate friend groups. Pairs keep arriving over time. Which approach fits best?

  1. Union-find (disjoint set union)
  2. Sorting the pairs by person, then scanning
  3. Sliding window
  4. Dynamic programming
Show answer

Answer: A. Union-find (disjoint set union)

Clue: "connected groups" with merges arriving incrementally. Each union merges two sets in nearly O(1) (with path compression and union by rank), and you track the group count as you go. BFS/DFS also works on a fixed graph but must re-run when edges are added.

13. Binary searchMedium

Find a target in a sorted array that was rotated at an unknown point, e.g. [4,5,6,7,0,1,2], in O(log n). Which approach fits?

  1. Binary search, checking which half is sorted at each step
  2. Linear scan, since rotation breaks sorted order
  3. Hash map from value to index, built once
  4. Sort the array back into order, then binary search
Show answer

Answer: A. Binary search, checking which half is sorted at each step

Clue: "sorted" + O(log n) required. At every step, compare nums[lo] with nums[mid] to find which half is in order, then decide whether the target lies in that half.

14. GreedyMedium

Attend as many non-overlapping meetings as possible from a list of (start, end) times. Which strategy is optimal?

  1. Pick the meeting that ends earliest, repeatedly
  2. Pick the shortest meeting first
  3. Pick the meeting that starts earliest, repeatedly
  4. Try every combination
Show answer

Answer: A. Pick the meeting that ends earliest, repeatedly

Sort by end time and take each meeting that starts after the last chosen one ends. Finishing earliest leaves the most room for the rest; an exchange argument proves it. O(n log n).

15. HeapMedium

Return the K most frequent words in a large list. Which approach fits best?

  1. Count with a hash map, then keep a min-heap of size K
  2. Sort the words alphabetically, then scan for the longest runs
  3. Two pointers from both ends
  4. Binary search over the words
Show answer

Answer: A. Count with a hash map, then keep a min-heap of size K

Clue: "top K" by some score. Counting is O(n); a size-K heap over the distinct words is O(m log K). Bucket sort by frequency (index = count) gets it to O(n) if asked for better.

Practise like this every day

InterviewGrind gives you ten questions like these each day on your iPhone, with Pro tracks for company-style loops.

Get the launch email