- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- How to Answer in an Interview
Interview Patterns
How to Answer in an Interview
You are not being marked on whether you produce the optimal solution. You are being marked on whether working with you would be pleasant and productive. Those are different tests, and the second one is easier to pass.
The sequence
- Restate the problem. One sentence. It catches misunderstandings while they are still free.
- Ask about the edges. Empty input? Duplicates? Negative numbers? Is it sorted? How large can it get?
- Say the brute force out loud, with its complexity. "I could check every pair, that is O(n squared)." You now have a working answer banked.
- Name the bottleneck. "The inner loop repeats a search I could do in constant time."
- Improve that one thing, and say what it costs. "A hash map makes it O(n) time and O(n) space."
- Then write the code.
- Test it out loud on a small case and an edge case.
Most people skip steps 1 to 5 and start at 6. That is why they solve the wrong problem, or freeze.
Say the complexity unprompted
Every solution should come with its cost, stated without being asked:
"This is O(n) time and O(n) space. I could get to O(1) space with two pointers if the array were sorted, but sorting would make it O(n log n) overall, so this is better unless the input arrives sorted."
That sentence demonstrates more than the code does. It shows you know the trade you made and why.
When you are stuck
- Say so, and say what you have tried. Silence reads as having nothing; narration reads as thinking.
- Do a tiny example by hand. The pattern often becomes visible once you see three cases written out.
- Ask what you can assume. Sorted? Fits in memory? Positive only? The constraints are hints.
- Write the brute force anyway. A working slow solution scores far above an unfinished elegant one.
Test your own code
What to check before you say you are done
javascript
function twoSum(nums, target) {
const seen = new Map()
for (let i = 0; i < nums.length; i++) {
const partner = target - nums[i]
if (seen.has(partner)) return [seen.get(partner), i]
seen.set(nums[i], i)
}
return []
}
// Walk these out loud:
console.log(twoSum([2, 7, 11, 15], 9)) // [0, 1] — normal
console.log(twoSum([], 5)) // [] — empty
console.log(twoSum([3], 3)) // [] — single element
console.log(twoSum([3, 3], 6)) // [0, 1] — duplicates
console.log(twoSum([-1, -2], -3)) // [0, 1] — negativesFinding your own bug before the interviewer does is a strong signal. Being told about it is not fatal either - how you react to it is what is being watched.
The thing worth remembering
An interviewer is imagining working with you on a hard problem at 5pm on a Friday. Communicating clearly, admitting uncertainty, and reasoning out loud matter more than arriving at the optimal answer in silence.
