Don't start typing. Run the same five-step routine on every problem, so you always have a next move, even before you know the answer.
Think of it like this: a pilot's checklist before take-off. Even very experienced pilots read the same list every time, because it catches the things that nerves make you skip.
Why a routine helps
Freezing at "where do I even start?" is normal. It usually happens because you try to jump straight to the clever answer.
A routine gives you something useful to do for the first five minutes. Each step produces something you can say out loud, and the interviewer counts all of it. By step 5 you'll often see the pattern without having to be clever.
Step 1: Restate the problem
Say it back in your own words, in one or two sentences. "So I get an array of numbers, and I return the length of the longest stretch where…"
This catches misunderstandings before you've spent 15 minutes on the wrong problem. If you can't restate it, ask a question.
Step 2: Inputs, outputs and constraints
Write down exactly what comes in and what goes out, then ask about the edges. Good questions:
- Can the input be empty? What should I return then?
- Can there be negative numbers, zeros or duplicates?
- Is the array sorted? This one changes everything.
- How big can the input get?
- Do I return a value, indexes, or a new array? Can I modify the input?
- What if there's no answer? Return -1, null, or an empty array?
The size tells you how fast your solution needs to be:
n up to ~20 O(2^n) is fine (try every combination)
n up to ~1,000 O(n²) is fine (nested loops)
n up to 100,000 or so you need O(n) or O(n log n)
"in O(1) extra space" no Map or copy: work in place or with pointers
Working in place means changing the input array itself instead of making a new one.
Step 3: Work small examples by hand
Take the given example and solve it on paper, slowly. Notice what you did with your eyes and hands. That's often the algorithm.
Then make up 2 or 3 edge cases: inputs at the boundaries where code tends to break.
- empty input, one element
- all the same value, all different
- the answer at the very start or the very end
- negatives or zeros, if allowed
These become your test cases later.
Step 4: Say the brute force and its Big-O
The brute force is the most direct solution: try every possibility. It's usually nested loops.
Say it out loud even if it's slow: "The simple way is to check every pair, which is O(n²)." This shows you can solve it, and it gives you a correct baseline to improve on. If you get stuck later, you can code the brute force and still have a working answer.
Step 5: Find the waste, then name a pattern
Look at the brute force and ask: what work am I doing more than once? Each kind of waste has a fix:
- Searching the array again and again for a value → remember values in a hash map or set.
- Re-adding almost the same range of elements on every step → a sliding window keeps a running total.
- Ignoring that the input is sorted → two pointers or binary search.
- Solving the same smaller problem many times → dynamic programming stores each answer, so you solve each smaller problem only once.
Then say the pattern: "Because the inner loop is searching for one value, I'll use a map to make that lookup O(1)."
The routine on a real problem
Problem: an array holds n different numbers taken from 0 to n, so exactly one number in that range is missing. Return the missing one.
- Restate: "I get n distinct numbers from the range 0..n, and one value in that range isn't there. I return it."
- Inputs and outputs: an array of length n in, one number out. Ask: is it sorted? No. Can n be 1? Yes.
- Examples by hand:
[3, 0, 1]→ the range is 0..3, and 2 is absent, so 2. Edge cases:[0, 1]→ 2 (missing the last value).[1]→ 0 (missing the first value). - Brute force: for each candidate from 0 to n, check
nums.includes(candidate). That's n + 1 candidates times an O(n) search: O(n²). - Waste and pattern:
includessearches the whole array every time. Put the numbers in a Set once, then each check is O(1). Total: O(n) time, O(n) space.
The interviewer may then ask "can you do it in O(1) space?" One answer: add up 0..n with the formula n × (n + 1) / 2, and subtract the actual sum. The difference is the missing number.
function missingNumber(nums: number[]): number {
const present = new Set(nums); // O(n) once
for (let candidate = 0; candidate <= nums.length; candidate++) {
if (!present.has(candidate)) return candidate; // O(1) each check
}
return -1; // can't happen if the input follows the rules
}
If the prompt says … consider …
Phrases in the prompt are strong clues. Here's a map from typical wording to the patterns in this bank. Each pattern has its own lesson.
"two numbers that add up to", "seen before", "count",
"how many times", "anagram", "group by", "duplicate"
-> hash map or set
"sorted array", "pair in a sorted array", "palindrome",
"in place", "remove duplicates", "O(1) extra space"
-> two pointers
"contiguous", "subarray", "substring", "longest / shortest
... with at most k", "every window of size k"
-> sliding window
"valid brackets", "matching", "next greater element",
"undo", "most recent", "nested expression"
-> stack
"sorted" + "find", "first / last position", "O(log n)",
"smallest value that works", "minimum capacity / speed"
-> binary search
"intervals", "meetings", "overlapping", "merge ranges",
"schedule", "rooms"
-> sorting and intervals
"linked list", "node.next", "middle node", "cycle",
"reverse the list", "merge two sorted lists"
-> linked list
"binary tree", "root", "depth", "level by level",
"path from root to leaf", "ancestor"
-> trees (depth-first or breadth-first search)
"grid", "islands", "connected", "neighbours", "dependencies",
"prerequisites", "fewest steps"
-> graphs (breadth-first search, depth-first search,
topological sort)
"k largest / smallest", "top k", "k closest", "k most
frequent", "median of a stream", "merge k sorted"
-> heap
"number of ways", "minimum cost to reach", "can you make /
reach", "longest increasing", "max without picking neighbours"
-> dynamic programming
A clue is a starting point. Confirm it with your hand-worked examples before committing.
Common mistakes
- Starting to type in the first minute. You end up coding the wrong problem, or freeze halfway with nothing to show.
- Skipping the edge cases in step 3. The solution then breaks on an empty array or a single element, and you find out from the interviewer.
- Hiding the brute force because it's slow. Saying it out loud earns credit and gives you a fallback.
- Going silent while thinking. Narrate each step, even "I'm checking whether sorting would help".
- Ignoring the input size. With n up to 100,000, an O(n²) plan needs rethinking before you write it.