How to Approach a Coding Round
The 8-step script
Section titled “The 8-step script”Use this for every problem, even easy ones. Repetition makes it automatic, and automatic is what saves you when you’re nervous.
| # | Step | Time (45 min round) | What you actually do |
|---|---|---|---|
| 1 | Clarify | 2–3 min | Restate the problem. Ask about input size, types, duplicates, negatives, empty input, return format. |
| 2 | Examples | 2–3 min | Walk through the given example by hand. Make one small edge-case example of your own. |
| 3 | Brute force | 2–3 min | Say the naive solution and its complexity. Don’t code it unless asked. |
| 4 | Optimize | 5–10 min | Find the bottleneck. Match it to a pattern (see the table below). Get a nod before coding. |
| 5 | Code | 10–15 min | Write clean code. Narrate as you go. Use helpful names. |
| 6 | Dry-run | 3–5 min | Trace your code (not your idea) line by line on a small example. |
| 7 | Complexity | 1 min | State time and space, and say why. |
| 8 | Edge cases | 2 min | Empty, single element, all same, negatives, overflow, already sorted. |
Phrases to say out loud
Section titled “Phrases to say out loud”Clarifying
- “Just to make sure I understand: given ___, I need to return ___. Right?”
- “Can I assume the input fits in memory / is non-empty / has only lowercase letters?”
- “What’s the rough size of n? That will help me decide on the approach.”
- “If there are multiple answers, can I return any one?”
- “Should I handle invalid input, or can I assume it’s valid?”
Starting
- “Let me start with brute force so we have a baseline, then I’ll optimize.”
- “The brute force would check every pair, so O(n²). With n up to 10^5 that’s too slow, so I need something around O(n log n) or O(n).”
Optimizing
- “The repeated work here is ___. Can I cache that with a hash map?”
- “Since the array is sorted, this smells like binary search or two pointers.”
- “I’m trading space for time here. Is O(n) extra space okay?”
When stuck (say something, never go silent)
- “Let me think out loud for a second. I’m stuck on how to avoid recomputing ___.”
- “Let me try a smaller example to see the pattern.”
- “What if the input were sorted? Would that help?”
- “I think I’m missing something. Could I get a small hint about the direction?” (Asking for a hint costs less than 10 minutes of silence.)
While coding
- “I’ll write a helper for this part to keep the main logic clean.”
- “I’ll come back to this edge case after the main logic works.”
Recovering from a mistake
Section titled “Recovering from a mistake”Everyone makes mistakes. What gets scored is how you handle them.
- Say it calmly: “Wait, I think there’s a bug here. When the array is empty, this index goes out of bounds.”
- Explain the fix before you type it: “I’ll add a guard at the top that returns 0.”
- Re-run the failing case to show it’s fixed.
- If your whole approach is wrong: “This approach doesn’t handle ___. Let me step back. I think ___ works better because ___.” Changing direction on purpose is a good signal, not a bad one.
Don’t apologize over and over. Don’t erase everything in a panic. Fix it the way you’d fix a bug at work.
What interviewers score
Section titled “What interviewers score”| Signal | What they look for |
|---|---|
| Problem solving | Did you break the problem down, think about trade-offs, and reach a good solution, even with hints? |
| Communication | Could they follow your thinking? Did you ask good clarifying questions? |
| Coding | Clean, readable, correct code. Good names. No huge repeated blocks. |
| Testing / verification | Did you dry-run and find edge cases yourself? |
| Complexity | Can you state and justify time/space? |
| Coachability | When given a hint, did you pick it up and use it? |
Constraints tell you the approach
Section titled “Constraints tell you the approach”Roughly 10^8 simple operations run in about 1 second. Use n to work out the target complexity.
| Input / constraint | Likely approach | Target complexity |
|---|---|---|
| n ≤ 10–12 | Permutations, brute force | O(n!) |
| n ≤ 20 | Backtracking, subsets, bitmask | O(2^n) |
| n ≤ 100–500 | Nested loops, 3D DP | O(n³) |
| n ≤ 10^3 – 5·10^3 | 2D DP, all pairs | O(n²) |
| n ≤ 10^5 – 10^6 | Sorting, heap, binary search, hashing, two pointers | O(n log n) or O(n) |
| n ≥ 10^9 / huge values | Math, binary search on the answer | O(log n) or O(1) |
| Sorted array | Binary search, two pointers | O(log n) / O(n) |
| “Find pair/triplet with sum” | Hash map, or sort + two pointers | O(n) / O(n²) |
| “Contiguous subarray/substring” | Sliding window, prefix sum | O(n) |
| “Top/kth largest/smallest” | Heap (PriorityQueue), quickselect | O(n log k) |
| “Next greater/smaller”, “span” | Monotonic stack | O(n) |
| Tree | DFS (recursion) or BFS (queue) | O(n) |
| Grid / network / dependencies | BFS / DFS / topological sort | O(V + E) |
| Shortest path, unweighted | BFS | O(V + E) |
| Shortest path, weighted | Dijkstra | O(E log V) |
| “All combinations/permutations” | Backtracking | Exponential |
| “Count ways / min cost / max profit” with choices | Dynamic programming | Usually O(n) – O(n²) |
| “Minimize the max” / “smallest x such that…” | Binary search on answer | O(n log range) |
| Intervals | Sort by start, then sweep | O(n log n) |
| Connected groups, merging sets | Union-Find or DFS | ~O(n) |
A tiny pre-round checklist
Section titled “A tiny pre-round checklist”- Keep a Java skeleton in your head:
class Solution { public int solve(int[] nums) { ... } }. - Know your imports:
java.util.*. - Know these by heart:
HashMap.getOrDefault,Arrays.sort,PriorityQueuewith a comparator,ArrayDequeas a stack and queue,StringBuilder,Collections.reverse. - Drink water. Slow down your first sentence. The first 2 minutes set the tone.