Sorted input is a cue, but which patterns does it license, and what decides between them?
answer
- sortedness is an invariant, not a pattern
- let the ask choose, not the guarantee
- pairs, boundaries, or two ordered sources
- halving assumes you can jump to the middle
- an unsorted input must pay for the order first
basics
~20 sSorted input licenses three families: converging pointers for pair or combination asks, binary search for a boundary or threshold ask, and a merge pass for combining two ordered sources. The shape of the ask, not the sortedness, picks one.
solid answer
~50 sSortedness is an invariant, and the pattern depends on what you do with it. If the ask is *a pair or combination hitting a target relation*, converging pointers work: one comparison at the two ends shows which end can never improve, so you retire it and move inward — O(n), O(1) extra space. If the ask is *a boundary* — the first entry meeting a threshold, or the smallest feasible answer over a monotone predicate — that is binary search, O(log n), and it applies to answer spaces as well as stored elements. Two ordered sources call for a linear merge. Two cost checks decide the rest: sorting is not free, so if a hash pass answers it in expected O(n) and ordered output is not required, sorting is a downgrade; and halving needs random access, which an ordered sequence you can only walk does not give you.
go deeper
Be ready to say that sorted input opens more than one door, and to name at least two: pointers converging from the ends, and a search for a boundary position. Knowing which one your ask needs is the point.
Explain the mechanism: why one comparison at the ends retires an endpoint, and why halving needs a monotone property plus constant-time access to the middle. Then say what the ask has to look like for each.
Show cost judgment. State when paying O(n log n) to create order is worth it versus an expected O(n) single pass, and call out that sorting discards original positions unless you carry them.
Own the framing that a guarantee in the statement is a resource with a price. Be ready to argue when the team should sort once and serve many queries versus answering each query independently, and what that choice costs at ten times the data.
## The cue and the three families "The input is sorted" is the single most over-read cue in coding interviews, and the standard wrong answer is "sorted means binary search." Sortedness is not a pattern; it is an **invariant** — for any position, everything to the left is no larger and everything to the right is no smaller. What you can do with that invariant depends entirely on the ask. **Family 1 — converging pointers (the ask is a pair or a combination).** Place one index at each end. Evaluate the current pair against the target relation. Because the sequence is ordered, a single comparison proves that one of the two current endpoints cannot participate in any better pair, so you retire it and move inward. Example ask, in an original setting: a shipping manifest is ordered by crate weight, and you want the pair of crates whose combined weight comes closest to the container's remaining capacity without exceeding it. If the current pair is over capacity, the heavy end cannot pair with anything lighter than the current light end and still improve, so it moves inward; otherwise you record the candidate and advance the light end for a bigger total. Each index moves at most n steps, giving O(n) time and O(1) extra space. Extending to a three-crate combination is the same idea with an outer loop: O(n^2), not O(n^3). **Family 2 — binary search (the ask is a boundary).** Use it when the question is *where does a property start being true*: the first crate at or above a weight, the count of entries below a threshold (found from two boundaries), or the smallest capacity that makes a whole plan feasible. The last case matters more than candidates expect: binary search needs a **monotone predicate**, not a stored sorted array. If "capacity C works" implies every larger capacity works, you can binary search the answer space itself, calling a feasibility check as the comparison. That is where the pattern earns its keep beyond simple lookup. **Family 3 — a merge pass (the ask combines two ordered sources).** Two ordered manifests, and you want their combination, their overlap, or a rank statistic across both. Advancing whichever front element is smaller costs O(n + m); re-sorting the concatenation costs O((n+m) log(n+m)) and throws away the order you were handed. ## What actually decides The **shape of the ask** decides: - pair / triple / combination against a target relation → converging pointers; - first-or-last position satisfying a monotone property, or a numeric answer with a feasibility test → binary search; - two or more ordered sources to reconcile → merge. Two further checks separate a middle answer from a junior one: **Sorting is not free, and it destroys information.** If the input is *not* already sorted, choosing a sorted pattern means paying O(n log n) up front. For an existence question like "do two entries stand in this exact relation," a single pass with a hash map of seen values answers it in expected O(n) — sorting is then a downgrade unless you need ordered output, unless you must avoid the hash map's memory, or unless you need a worst-case guarantee that hashing does not give you (hash operations are expected O(1), degrading to O(n) per operation under adversarial collisions). Sorting also destroys the original positions: if the answer must be reported as positions in the input, you must carry them along explicitly. **Binary search needs random access.** Halving assumes you can jump to the middle in constant time. An ordered sequence you can only traverse one link at a time gives you converging pointers only if you can reach both ends cheaply, gives you a merge pass, and gives you no halving at all — the jump to the midpoint is itself linear, which cancels the benefit. "Sorted, therefore binary search" quietly assumes an access model the statement may not have granted. ## Duplicates and the boundary you actually want With repeated keys, "find the entry equal to x" is under-specified: you usually want the *first* position at or above x, or the *first* position strictly above x. Writing the search to return a boundary rather than an arbitrary match makes counting, range extraction and insertion-point queries fall out of the same routine, and it removes the most common off-by-one in the pattern. Say which boundary you are computing before you write the loop. ## The interview answer When an interviewer plants "the input is sorted" in the statement, they are testing whether you treat it as a verdict or as a resource. The strong answer names the family the *ask* selects, states the cost of the alternative that ignores sortedness, and flags the access-model assumption. That is a thirty-second narration and it distinguishes practiced recognition from memorised solutions.
- The input is unsorted and the ask is an existence check on a pair relation. Sort first, or not?Usually not. A single pass storing seen values in a hash map answers it in expected O(n) time and O(n) space, while sorting costs O(n log n) and discards original positions. Sorting wins when you need ordered output anyway, when memory is tight enough that the hash map is unaffordable, when you need a worst-case rather than expected bound, or when the same ordered data will serve several later queries.
- Where does binary search apply when there is no sorted array at all?Wherever a predicate is monotone over a range of candidate answers: if a candidate value works then every larger one works. You binary search the answer space and use a feasibility check as the comparison — smallest capacity, smallest deadline, smallest threshold. The cost becomes O(log R) feasibility checks over the candidate range R, which is why it stays attractive even when each check is linear.
- With duplicate keys, what should a search over sorted data return?A boundary, not an arbitrary match: the first position at or above the key, or the first position strictly above it. Boundaries compose — counting occurrences is the difference between the two, and an insertion point is the first of them — while an arbitrary matching position answers none of those questions and invites off-by-one errors at the ends.
saying these in an interview costs you the question
- Says sorted input always means binary search
- Sorts an unsorted input for a question a single hash pass answers
- Assumes halving works on a sequence without random access
- Forgets that sorting discards the original positions
- Searches for any match instead of a boundary with duplicates