</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Sequences and Queries

What is this?

These are the trickiest, most satisfying uses of binary search โ€” the ones where you're not searching for a value you can see at all. You might be hunting the kth missing number, the dividing line between two sorted lists, or who was winning an election at some moment in time. The shared move is to invent a quantity that only ever grows as you move along, then binary-search the exact point where it crosses the target. A common companion trick is to do one slow pass up front to build a lookup, so that afterwards a flood of questions each get answered almost instantly.

flowchart TD A["Build a quantity that only grows"] --> B["Binary-search where it crosses the target"] B --> C["Precompute once for many queries"] C --> D["Answer each query in log time"]

๐Ÿ’ก Fun fact: The "do the heavy work once, then answer each question fast" idea here is the same one behind database indexes โ€” building the index is slow, but afterward millions of lookups fly, which is why a search that crawls without one can run thousands of times slower.

๐Ÿ”“ The 5 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: Binary search can locate things that are not values at all โ€” a count of missing numbers, a partition point, the right moment in a timeline. The trick is to define a quantity that increases monotonically with the index (or the candidate), then binary-search the place where that quantity crosses your target. These are the hardest, most rewarding applications of the technique.


The pattern

Each problem here builds a monotonic signal and searches it. For "missing element" questions, the number of values missing before index i only grows as i grows, so you binary-search for where the missing-count reaches your target. The same gap-counting idea answers "the kth missing positive" by treating absent numbers as a running deficit.

A second idea is partition search: rather than merging two sorted arrays, binary-search the cut that splits the combined elements into a correct left and right half โ€” that is how the median falls out in log time without ever merging.

A third idea is precompute, then query: do one linear pass to build a structure (a leader-over-time timeline, prefix counts of an item), then answer each query with a binary search or an array lookup. The work moves from per-query to one-time setup.


The problems

Missing Element in Sorted Array โ€” find the kth missing value. Count missing before index: at index i, missing-so-far is nums[i] โˆ’ nums[0] โˆ’ i; binary-search the first index where that count reaches k, then offset into the gap.

Kth Missing Positive Number โ€” find the kth absent positive integer. Same gap counting: at index i, the missing count is nums[i] โˆ’ (i + 1); binary-search where it first reaches k.

Median of Two Sorted Arrays โ€” the classic O(log(min(m, n))) problem. Partition search: binary-search a cut in the smaller array that, paired with the complementary cut in the larger, puts the right counts on each side and satisfies the order condition โ€” the median sits at the boundary.

Online Election โ€” answer "who led at time t?" repeatedly. Precompute leaders in one pass over the votes, store the time of each lead change, then bisect time to find the leader for each query in O(log n).

Plates Between Candles โ€” count plates strictly between two candles for many queries. Build prefix counts of plates and, for each query, find the nearest candle inward on each side (precomputed or via binary search), then subtract prefix counts.


Key takeaways

  • Search a monotonic quantity, not the raw array. Missing-counts, partitions, and lead-times all grow predictably โ€” binary-search the crossing point.
  • Gap counting answers "missing" questions. A simple formula at each index turns absence into a searchable count.
  • Partition instead of merge. The median comes from finding the right split, never from combining the arrays.
  • Precompute once, query many. Build a timeline or prefix array up front so each query is O(log n) or O(1).
  • Why this chapter matters: it stretches binary search to its limits, the level expected for hard interview rounds and the foundation for query-heavy problems.

Core idea: at every index the array "owes" you a known number of missing values, and that debt only grows โ€” so binary-search for the spot where the debt first reaches k.

Problem, rephrased

You run a system that hands out sequential positive ID numbers starting at 1. Some IDs got claimed; the claimed ones are recorded, in strictly increasing order, in a list arr. Every positive integer not in arr is a free ID. Given arr and a number k, return the kth free ID (counting free IDs from smallest to largest, 1-indexed).

The free IDs are simply the positive integers 1, 2, 3, โ€ฆ with the ones in arr crossed out. You want the kth survivor.

arr k Free IDs (in order) Answer
[2, 3, 4, 7, 11] 5 1, 5, 6, 8, 9, 10, 12, โ€ฆ 9
[1, 2, 3, 4] 2 5, 6, 7, โ€ฆ 6
[3, 5, 8] 1 1, 2, 4, 6, 7, โ€ฆ 1

Notice in the last row the answer (1) comes before the whole array. That case matters โ€” we'll keep checking it.

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.