INTERVIEW MASTERY / ALGORITHM BRIEF

Coding under time pressure

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.

IntermediatePhase 09 / Topic 5 of 6Mental modelComplexityEdge cases
01

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 chef during dinner rush

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.

02

When to use it

  • Timed coding interviews and online assessments.
  • Contests and hackathons.
  • Any situation where you must implement correctly on the first try.
03

Problem patterns it solves

Template-first problems

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
Helper decomposition

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)
Partial credit strategy

Recognize it when: running out of time: working brute force beats broken optimal.

  • Any problem where the optimal approach is unclear
04

Where it is used in real software

On-call incident fixes

Engineers under pressure rely on runbooks and known patterns, write the smallest safe fix, and verify before deploying.

Pair programming

Narrating intent while coding keeps partners aligned, exactly like thinking out loud in interviews.

Time-boxed spikes

Teams time-box exploratory work and ship the simplest working version first.

05

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.
06

A 45-minute plan

  1. 1
    0-5 min: clarify

    Restate, ask about constraints and edge cases, agree on an example.

  2. 2
    5-15 min: design

    Brute force, then the optimal idea with complexity. Get interviewer buy-in before coding.

  3. 3
    15-35 min: code

    Write the template first, top-down with helpers, narrating briefly. Use clear names.

  4. 4
    35-42 min: test

    Dry-run the example and 2 to 3 edge cases; fix bugs by tracing, not guessing.

  5. 5
    42-45 min: wrap up

    State final complexity, mention improvements or follow-ups.

07

Debugging routine when the output is wrong

Your sliding window returns 4 instead of 3 for "abcabcbb"

Step 1 / 5
StepActionFinding
1Pick the smallest failing input"abca" also returns 4
2Trace variables line by lineat right = 3, left should move to 1 but stays 0
3Locate the faulty lineshrink condition checks the wrong character
4Fix and re-run the trace"abca" returns 3
5Re-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.

08

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;}
09

Complexity and performance

Coding window15-20 min

For a medium problem.

Testing window5-7 min

Example plus edge cases.

Templates to master8-10

Binary search, BFS, DFS, window, backtracking, DP, heap, union find, topo sort.

10

Trade-offs

Speed vs clarity

Readable names and helpers cost seconds and save minutes of debugging; single-letter variables save little and hurt communication.

Narrating vs focusing

Narrate intent at the start of each block, then code quietly; long silent stretches worry interviewers, constant chatter slows you down.

11

Variants and related techniques

Online assessments

No interviewer: read all problems first, solve the easiest fully, and use brute force for partial scores.

Whiteboard or plain editor

No autocomplete or compiler: rely on templates and careful dry runs.

12

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.

13

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.

14

Practice problems

ProblemDifficultyWhat it trains
Rewrite 8 templates from memoryEasySpeed and correctness.
Timed set: 2 mediums in 45 minutesMediumBudgeting.
934. Shortest BridgeMediumTop-down with helpers.
Debug a planted bug in a partner's codeMediumTracing routine.