
Coding Interviews: A Repeatable Approach
This lesson is for anyone heading into a coding round, from a first internship screen to a senior software or data role. It gives you one method you can run on any problem, shows it on a complete example, and covers what to say while you work. The goal is not to have seen every problem before. It is to look calm and methodical on one you have not.
How the coding interview works
On SmartIntervu a coding interview runs in a code editor with a voice interviewer. You read the problem, talk through it, write code, and run it. If you stall, the interviewer can offer a hint. The report then scores you on five things:
Problem solving: did you clarify the problem, explore examples, reach a working approach and reason about its complexity before coding?
Correctness: does the final code solve the problem, and when a run fails, do you understand and fix why?
Code quality: readable structure, clear names, idiomatic use of the language, nothing needlessly clever.
Testing and edge cases: do you name edge cases without being asked, trace the code with an example, and fix what that reveals?
Communication: can the interviewer follow your thinking the whole way?
Notice that only one of the five is "the code works". A candidate who reaches a correct answer silently, with no complexity analysis and no tests, leaves most of the score on the table. A candidate who reaches the optimal answer a little late but explains every step usually does better.
The method, with a time budget
For a 45-minute round, aim for roughly this split. It flexes, but if you are 20 minutes in without a plan, you know you are behind.
Understand (about 5 minutes). Restate the problem in your own words. Ask about input size, value ranges, duplicates, empty input, and what to return when there is no answer. Work one small example by hand.
Plan (about 10 minutes). State the brute-force approach and its complexity first, even if you will not code it. Then look for the better approach, say its complexity, and get a nod before writing anything.
Code (about 15 minutes). Write the planned solution in clean, small steps. Name variables for what they hold. Narrate the intent of each block, not every keystroke.
Test (about 10 minutes). Trace your code on the example by hand, then on the edge cases you listed in step 1. Run it. Fix what breaks and explain the fix.
Wrap up (about 5 minutes). Restate the final time and space complexity, and mention what you would change for a follow-up such as a stream of inputs or a much larger data set.
Let the constraints pick your target
Input size tells you what complexity the interviewer expects. A typical machine does on the order of 10^8 simple operations per second, so:
n up to about 10 to 20: exponential is fine (try every subset or permutation, backtracking).
n up to about 1,000: O(n^2) is fine.
n up to about 10^5 or 10^6: aim for O(n log n) or O(n).
n up to 10^9 or more: you cannot even read every value; think O(log n) (binary search) or O(1) (math).
If the problem gives no size, ask. "How large can the array get?" is a strong opening question because it shows you are already thinking about complexity.
Core patterns and the cue for each
Most interview problems are a known pattern in disguise. Learn the cue, not just the code. Each pattern below has its own lesson in the patterns series, with solved problems and the traps that cost points.
Hash map lookup. Cue: "find a pair or complement", "have I seen this before", counting. Trades O(n) memory for O(1) lookups.
Two pointers. Cue: a sorted array, or comparing both ends of a sequence (palindromes, pair sums in sorted input). Usually O(n) time and O(1) space.
Sliding window. Cue: "longest or shortest contiguous subarray or substring that satisfies a condition". Grow the right edge, shrink the left when the condition breaks.
Binary search. Cue: sorted data, or "find the smallest value that works" when you can test any guess cheaply. O(log n) per search.
Breadth-first search. Cue: shortest path in an unweighted graph or grid, or anything "level by level".
Depth-first search and backtracking. Cue: explore all paths, all combinations, connected regions, tree traversals.
Heap. Cue: "top k", "k-th largest", repeatedly taking the smallest or largest item. O(n log k) for top k.
Dynamic programming. Cue: "number of ways", "minimum cost", and choices where the same subproblem repeats. Define the state in words before writing a recurrence.
Tip
When you recognize a pattern, say so: "This looks like a sliding window because we want the longest contiguous substring." It earns problem-solving credit and lets the interviewer redirect you early if you are wrong.
Worked example: Two Sum
Problem: given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Exactly one answer exists, and you may not use the same element twice.
Understand
A strong candidate asks a few quick questions before anything else: Can numbers be negative? (Yes, it does not change the approach.) Can the same value appear twice? (Yes, for example [3, 3] with target 6, which is why we track indices, not values.) Does the order of the returned indices matter? (Assume not.) How large is the array? (Say up to 10^5.)
That last answer matters: 10^5 elements means O(n^2) is about 5 x 10^9 pair checks in the worst case, which is too slow. We need something closer to O(n).
Plan
Say the brute force out loud first: check every pair (i, j) with i < j. That is O(n^2) time and O(1) space. It is correct but too slow for n = 10^5.
Then improve it. For each number x, we need to know whether target - x appeared earlier. "Have I seen this before" is the hash map cue. Walk the array once, and for each element check the map for its complement before inserting the element itself. That gives O(n) time and O(n) space. Checking before inserting is what prevents using the same element twice.
Code
function twoSum(nums: number[], target: number): [number, number] {
// value -> index of an earlier element with that value
const seenIndexByValue = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
const j = seenIndexByValue.get(complement);
if (j !== undefined) {
return [j, i];
}
seenIndexByValue.set(nums[i], i);
}
throw new Error("No pair sums to target");
}Note the small choices that score on code quality: a name that says what the map holds, j !== undefined rather than a truthiness check (index 0 is falsy, so if (j) would be a bug), and an explicit error for the "no answer" case even though the problem promises one exists.
Test
Dry run on nums = [2, 7, 11, 15], target = 9:
i = 0, value 2, complement 7. The map is empty, so store2 -> 0.i = 1, value 7, complement 2. The map has2 -> 0, so return[0, 1]. Correct.
Then the edge cases you raised earlier:
Duplicates,
[3, 3]with target 6: ati = 0the map is empty, store3 -> 0; ati = 1the complement 3 is found at index 0, return[0, 1]. Correct, and it shows why we check before inserting.Negatives,
[-1, -2, -3, -4, -5]with target -8: the complement of -5 is -3, stored at index 2, so it returns[2, 4]. Correct.An element that is its own complement but appears once,
[4, 1, 5]with target 8: 4 is checked against an empty map, so it is never paired with itself. The loop ends without an answer, which the problem rules out, and the function throws rather than returning garbage.
Wrap up
"Final complexity is O(n) time and O(n) space. If memory were tight and we could reorder the data, I could sort a copy of the values with their original indices and use two pointers, for O(n log n) time and no hash map. If the input were a stream, the hash map version already works because it only looks backward."
What to say out loud
Narrating every keystroke is noise. Narrate decisions:
What you are about to try and why: "I'll use a map from value to index so the complement check is constant time."
When you are unsure: "I'm not sure whether we need the first pair or any pair. Let me ask."
When you change course: "This handles duplicates wrongly. I'll check before inserting instead of after."
When you finish a block: "That's the main loop. Now the case where nothing matches."
Short silences while you think are fine. Say "Give me a moment to think about the sorted case" and then think. What hurts is five silent minutes the interviewer cannot interpret.
Using hints well
A hint is not a failure. It is information. When the interviewer offers one, repeat it back in your own words, connect it to your current approach ("so instead of rescanning the array, I should remember what I've already seen"), and then move. Two things cost points: ignoring a hint and carrying on down the same path, and asking for hints before you have tried anything. If you are stuck, describe exactly where: "I have the O(n^2) version and I'm looking for a way to avoid the inner loop." That often gets you a better hint, and sometimes you find the answer while saying it.
Mistakes that cost points
Coding before the plan is agreed. You risk 15 minutes on the wrong approach. Instead, state the approach and complexity and wait for a nod.
Skipping the brute force. It shows you can always produce a correct answer and gives you a baseline to improve on. Mention it in a sentence, then move on.
Testing only the happy path. Interviewers watch for empty input, one element, duplicates, negatives and the largest size. List them during "Understand" and come back to them.
Off-by-one errors in loops and slices. Trace boundaries with a tiny example instead of trusting your memory.
Clever but unreadable code. A one-line reduce that nobody can follow scores lower than a clear loop. Optimize for the reader.
Not stating complexity at the end. Even if you said it during planning, restate it for the final code; it may have changed.
Practice it
Take ten minutes and write out the five steps for a problem you already know, with the time budget beside each. Then run a full interview with the method:
Two Sum: run the whole method on the example above, and see whether you can explain every choice without the notes.
Design an LRU Cache: a medium problem where planning the data structures before coding is most of the work.
Practise this in a mock interview
Try what this lesson covers out loud: the AI interviewer asks one of these questions, follows up, and scores your answer.
- Two SumCoding · Easy
- Design an LRU CacheCoding · Medium
- Deploy Services in Dependency WavesCoding · Medium
- Flag Overlapping Maintenance WindowsCoding · Medium
Read next
- AI-Assisted Coding Interviews: Directing, Checking and Owning the Code
Know how coding interviews that allow an AI assistant tend to run and what they grade, follow a seven-step workflow for prompting, reading and verifying generated code, and avoid the habits that sink candidates.
8 min read
- Two Pointers
Recognize when sorted input, in-place edits or a linked list call for two pointers, then apply the opposite-ends, reader-writer and fast-slow shapes and say why each pointer move is safe.
8 min read
- Sliding Window
Spot contiguous-range problems, run fixed and variable windows from one template, decide where to record the answer, and explain why the window breaks once values can be negative.
8 min read
- Binary Search
Write one boundary-search template without off-by-one bugs, derive exact, first, last and count searches from it, and binary search on the answer and on rotated arrays.
9 min read
