Data structures & algorithms · Linear consolidation lab
Reading a problem and naming the tool
This module adds no new algorithm. It trains the thirty seconds before you write code: read the constraint line, find the phrase only one tool answers, and say its name out loud.
Narrow six tools down to one →01 The idea
Problems are written to a template. Learn to read it.
By Stage 2 you already know binary search, two pointers, sliding window, monotonic stack and hash maps. The gap that costs marks is no longer writing them. It is opening a statement you have never seen and knowing, in half a minute, which of the five it wants. That is a reading skill, and it is trainable.
Every online assessment and interview question is written to the same shape: a description, a return line, and a constraint line. The setter chooses each word. When the statement says sorted or contiguous or positive, that word is there because the intended solution uses it. Your job is to spot which words are doing work and which are scenery.
02 Hand trace
One statement, four clues, one tool left
Here is the statement this whole lesson runs on. Given an array of n positive integers and a value k. Return the length of the longest contiguous subarray whose sum is at most k. 1 ≤ n ≤ 100000, 1 ≤ arr[i] ≤ 10000.
Six tools start on the table, one per box below: brute (two nested loops), hash (hash map), win (sliding window), 2ptr (two pointers converging on a sorted array), mono (monotonic stack), bsrch (binary search). A rose box is ruled out and stays ruled out. Watch which clue kills which box.
Notice the order you read in. The constraint line is at the bottom of the statement and you read it first, because n ≤ 100000 squared is 10¹⁰ operations against a budget of roughly 10⁸ per second — about a hundred seconds for a one-second limit. That single line deletes brute force before you understand the question.
Then each phrase removes more. The length of the longest asks for one number for the whole array, so a monotonic stack — which produces one answer per index — has nothing to do. Contiguous is the loudest word: the elements must sit next to each other, so you may not sort, and both binary search and the converging two-pointer walk are sorted-only. That leaves a hash map and a window. Positive integers decides it: every value is at least 1, so widening the window can only raise the sum and shrinking from the left can only lower it. That is exactly the rule a window needs. You never had to recall a similar problem.
One honest footnote, because a tell names a favourite, not the only legal move. Binary search is not completely dead here. Every value is at least 1, so the prefix sums of this array come out strictly increasing — a sorted range appears that was never in the input — and you could binary search it for each right edge and get a correct answer in O(n log n) time and O(n) memory. It would pass. The window is cheaper on both axes, so the trace rules binary search out as the intended tool, not as an impossible one. Say the alternative out loud in an interview; being able to price two correct solutions is worth more than producing one.
03 The procedure
Four questions, always in this order
The trace was one statement. Here is the general routine it followed, and then the lookup table that turns a phrase into a tool. Ask the four questions in this order every time — the order matters, because question one is the cheapest and eliminates the most.
The recognition table
| Phrase in the statement | What it is telling you | Tool | Typical cost |
|---|---|---|---|
| “sorted”, “non-decreasing”, “in increasing order” | Order is free information the setter expects you to spend. | Binary search to find one thing; two pointers converging from both ends to find a pair. | O(log n) / O(n) |
| “contiguous subarray”, “substring”, with a sum or length condition | The answer is one span, and both of its edges only ever move forward. | Sliding window | O(n) |
| “next greater”, “previous smaller”, “days until a warmer one”, “span” | Each index is answered by the nearest unresolved index behind it. | Monotonic stack | O(n) |
| “seen before”, “count”, “frequency”, “distinct”, “duplicate” | Position does not matter; membership and counts do. | Hash map or hash set | O(n) average |
| “a pair adding to X” on an unsorted array | You need a complement lookup, not a scan. | Hash set, or sort first and then converge two pointers | O(n) / O(n log n) |
| 1 ≤ n ≤ 100000 with a one-second limit | Anything quadratic is already too slow, whatever the wording says. | Forces a single pass or a sort | O(n²) is out |
05 Cheat sheet
The constraint ladder
Assume a judge does roughly 10⁸ simple operations per second. Find your n on the left and read across — that is the most expensive thing you are allowed to write.
| n up to | Complexity that fits | Steps at that n | What you would reach for |
|---|---|---|---|
| 10 | O(n!) | 3,628,800 | Permutations, full enumeration, backtracking |
| 20 | O(2ⁿ) | 1,048,576 | Every subset, bitmask DP |
| 500 | O(n³) | 125,000,000 | Three nested loops, Floyd–Warshall |
| 5000 | O(n²) | 25,000,000 | Two nested loops, simple DP table |
| 100000 | O(n log n) | ~1,700,000 | Sort, binary search, heap |
| 1000000 | O(n) | 1,000,000 | Sliding window, hash map, monotonic stack |
| 1000000000 | O(log n) | ~30 | Binary search on the answer, direct maths |
06 Where & why
A tell is a hypothesis, not an answer
Reading tells gets you to a candidate in thirty seconds. It does not prove the candidate is correct, and it is not a substitute for knowing the five tools. What it buys you is a direction, so the rest of your thinking goes into checking one precondition instead of staring at a blank page.
Reach for it when…
Use something else when…
| Situation | Better choice | Why |
|---|---|---|
| The tell fires but the precondition fails — “contiguous sum” with values that can be negative | Prefix sums — with a hash map when the target sum is exact, with a monotonic deque when it is “at most k” | The window shrink rule assumes widening can only raise the sum. With negatives it can fall, so the left edge has no safe rule and the window is simply wrong: on 3, −2, 1 with k = 2 it returns 2 when the whole array is a legal answer of length 3. |
| No tell fires at all | Write the brute force, then remove its repeated work | A correct O(n²) scores; a half-remembered O(n) scores nothing. The brute force also shows you exactly which work to cache. |
| The constraint is small — n ≤ 2000 or n ≤ 20 | Nested loops, or full enumeration | A small limit is the setter saying the clever solution is not required. O(n²) at n = 2000 is 4 million steps and passes comfortably. |
| Two tools both fit the budget | The one with less extra memory, or the one you can write correctly under pressure | Equal time is not equal cost: O(1) space beats O(n) space, and a working window beats a buggy hash map. |
07 Interview questions
Say these out loud
These are asked as a live reading exercise: the interviewer reads a statement and watches how you narrow it. Practise the reasoning, not the answer.
How do you decide which technique a problem needs?
What does the constraint line actually tell you?
You see the phrase “contiguous subarray”. What do you reach for, and what do you check first?
What is the difference between two pointers and a sliding window?
When does a hash map beat sorting?
What phrase makes you think monotonic stack?
Longest subarray with sum at most 2: the window is right on 3, 2, 1 and wrong on 3, −2, 1. Why?
n is 2000 and the time limit is one second. What changes?
Sorted array — binary search or two pointers?
You read the statement and no tell fires. What do you do?
Is pattern recognition just memorising problems?
08 Practice problems
Name the tool. Do not write the code.
For each one, answer in a single sentence: the tool, the time cost, the space cost, and the precondition you checked. Writing the solution is not the exercise here — getting to the right name in under a minute is.