Overview
Coding under time pressure is the skill of turning an agreed approach into correct, readable code within about 15 to 20 minutes while explaining what you do. Knowing the algorithm is not enough; many candidates fail because they run out of time, get lost in details, or freeze when a bug appears.
The antidotes are structure and rehearsal: a fixed time budget for each phase, code templates you can write from memory (binary search, BFS, sliding window, backtracking, union find), top-down coding with helper functions, and a calm debugging routine that traces a small example instead of guessing.
A professional chef does not invent techniques during service. Ingredients are prepared in advance (mise en place), standard recipes are memorized, and when something goes wrong they follow a routine instead of panicking. Templates and a time plan are your mise en place.
When to use it
- Timed coding interviews and online assessments.
- Contests and hackathons.
- Any situation where you must implement correctly on the first try.
Problem patterns it solves
Recognize it when: the pattern is recognizable: write the template, then customize.
- 704. Binary Search
- 102. Binary Tree Level Order Traversal
- 3. Longest Substring Without Repeating Characters
- 78. Subsets
Recognize it when: the main function is long; split into named helpers.
- 79. Word Search (dfs helper)
- 208. Implement Trie (insert / search)
- 146. LRU Cache (unlink / addToFront)
Recognize it when: running out of time: working brute force beats broken optimal.
- Any problem where the optimal approach is unclear
Where it is used in real software
Engineers under pressure rely on runbooks and known patterns, write the smallest safe fix, and verify before deploying.
Narrating intent while coding keeps partners aligned, exactly like thinking out loud in interviews.
Teams time-box exploratory work and ship the simplest working version first.
Key terms
- Time budget
- Planned minutes for clarify, design, code, test.
- Template
- A memorized skeleton for a common pattern.
- Top-down coding
- Write the main flow with helper calls first, then implement helpers.
- Dry run
- Trace the code by hand on a small input.
A 45-minute plan
- 10-5 min: clarify
Restate, ask about constraints and edge cases, agree on an example.
- 25-15 min: design
Brute force, then the optimal idea with complexity. Get interviewer buy-in before coding.
- 315-35 min: code
Write the template first, top-down with helpers, narrating briefly. Use clear names.
- 435-42 min: test
Dry-run the example and 2 to 3 edge cases; fix bugs by tracing, not guessing.
- 542-45 min: wrap up
State final complexity, mention improvements or follow-ups.
Debugging routine when the output is wrong
Your sliding window returns 4 instead of 3 for "abcabcbb"
| Step | Action | Finding |
|---|---|---|
| 1 | Pick the smallest failing input | "abca" also returns 4 |
| 2 | Trace variables line by line | at right = 3, left should move to 1 but stays 0 |
| 3 | Locate the faulty line | shrink condition checks the wrong character |
| 4 | Fix and re-run the trace | "abca" returns 3 |
| 5 | Re-run the original and edge cases | "abcabcbb" -> 3, "" -> 0, "bbbb" -> 1 |
NOWStep: 1 | Action: Pick the smallest failing input | Finding: "abca" also returns 4
A systematic trace on the smallest failing case finds bugs faster than rereading code, and it shows the interviewer a professional debugging process.
Implementation
// Templates worth writing from memory // 1. Binary search on a monotonic predicate: first index where ok(i) is truefunction firstTrue(lo, hi, ok) { while (lo < hi) { const mid = lo + ((hi - lo) >> 1); if (ok(mid)) hi = mid; else lo = mid + 1; } return lo;} // 2. BFS on a gridfunction bfsGrid(grid, sr, sc) { const rows = grid.length, cols = grid[0].length; const dist = Array.from({ length: rows }, () => new Array(cols).fill(-1)); const queue = [[sr, sc]]; dist[sr][sc] = 0; for (let head = 0; head < queue.length; head++) { const [r, c] = queue[head]; for (const [dr, dc] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { const nr = r + dr, nc = c + dc; if (nr < 0 || nc < 0 || nr >= rows || nc >= cols) continue; if (grid[nr][nc] === 1 || dist[nr][nc] !== -1) continue; dist[nr][nc] = dist[r][c] + 1; queue.push([nr, nc]); } } return dist;} // 3. Variable sliding windowfunction longestValidWindow(arr, isValidAfterAdd, add, remove) { let left = 0, best = 0; for (let right = 0; right < arr.length; right++) { add(arr[right]); while (!isValidAfterAdd()) remove(arr[left++]); best = Math.max(best, right - left + 1); } return best;} // 4. Backtrackingfunction backtrackTemplate(choices, isComplete, isValid) { const result = [], path = []; (function dfs(start) { if (isComplete(path)) { result.push([...path]); return; } for (let i = start; i < choices.length; i++) { if (!isValid(path, choices[i])) continue; path.push(choices[i]); dfs(i + 1); path.pop(); } })(0); return result;}Complexity and performance
For a medium problem.
Example plus edge cases.
Binary search, BFS, DFS, window, backtracking, DP, heap, union find, topo sort.
Trade-offs
Readable names and helpers cost seconds and save minutes of debugging; single-letter variables save little and hurt communication.
Narrate intent at the start of each block, then code quietly; long silent stretches worry interviewers, constant chatter slows you down.
Variants and related techniques
No interviewer: read all problems first, solve the easiest fully, and use brute force for partial scores.
No autocomplete or compiler: rely on templates and careful dry runs.
Common mistakes
- Coding before the interviewer agrees with the approach.
Fix: Ask 'does this approach sound good before I code it?'.
- Random edits when a bug appears.
Fix: Trace the smallest failing input line by line.
- Spending 20 minutes on a stuck optimal idea.
Fix: Set a checkpoint: if not clear by minute 15, code the brute force and optimize after.
Interview questions
What do you do if you run out of time?
Explain clearly what remains: the missing part, how you would finish it, and the complexity. A clear plan for the remainder earns more credit than hurried broken code.
How do you practice for time pressure?
Solve problems with a timer, rewrite core templates from memory weekly, and do mock interviews where you must talk while coding.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Rewrite 8 templates from memory | Easy | Speed and correctness. |
| Timed set: 2 mediums in 45 minutes | Medium | Budgeting. |
| 934. Shortest Bridge | Medium | Top-down with helpers. |
| Debug a planted bug in a partner's code | Medium | Tracing routine. |