How to approach an interview problem
Interviewers grade the process as much as the code. Use the same steps every time.
- Clarify. Ask about input size, empty input, duplicates, negatives, and what to return when there is no answer.
- Examples. Work one small example by hand. Write it down as a test.
- Brute force first. Say the simple solution and its complexity, even if it is slow. A working slow answer beats a broken fast one.
- Improve. Ask what work repeats. Usually a set, a dict, sorting, or a pointer removes it.
- Test edge cases. Empty, one item, all equal, the answer at the end.
- State complexity. Time and extra space, and what
nis.
The program walks through a mini example: "does the list contain a duplicate?" The brute force is O(n²). The set version is O(n) time and O(n) extra space. Both pass the same edge cases.
Run it, then add your own edge case to cases.