Sorting Nearly Sorted Input
Problem
Sensor readings arrive over the network with timestamps, but because of independent buffering per sensor, a reading can appear up to k positions away from where it belongs in timestamp order. k is small (tens) and n is large (hundreds of millions). You must emit the readings in exact timestamp order.
- Define the property precisely and explain why a general-purpose O(n log n) sort is doing unnecessary work.
- Implement a sort whose cost depends on k rather than on n log n, working in a single streaming pass so you never hold the whole input.
- Compare it with insertion sort and with the language's built-in sort on this input, and say which you would actually ship.
- Input: an iterable of comparable items with the guarantee that each item's final sorted index is within k of its current index.
- Output: a generator yielding items in sorted order.
Examples
- k = 0: the input is sorted; any sensible method should be O(n).
- Input
[2, 1, 4, 3, 6, 5]with k = 1: every element is at most one position off. Output[1, 2, 3, 4, 5, 6]. - Input
[3, 1, 2, 6, 4, 5]with k = 2: a window of size k + 1 = 3 always contains the next smallest element. Output[1, 2, 3, 4, 5, 6]. - n = 10⁸, k = 20: the heap method does n log(k + 1) ≈ 4.4 × 10⁸ comparisons; a general sort does n log n ≈ 2.7 × 10⁹; insertion sort does at most nk = 2 × 10⁹ shifts but O(n + inversions) in practice, and inversions are at most nk.
Hints
- If every element is at most k positions from its home, where can the globally smallest element be?
- What structure gives you the minimum of a small, changing set in O(log k)?
- Insertion sort costs O(n + inversions). How many inversions can a k-sorted array have?
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.