How to approach an interview problem

Interviewers grade the process as much as the code. Use the same steps every time.

  1. Clarify. Ask about input size, empty input, duplicates, negatives, and what to return when there is no answer.
  2. Examples. Work one small example by hand. Write it down as a test.
  3. Brute force first. Say the simple solution and its complexity, even if it is slow. A working slow answer beats a broken fast one.
  4. Improve. Ask what work repeats. Usually a set, a dict, sorting, or a pointer removes it.
  5. Test edge cases. Empty, one item, all equal, the answer at the end.
  6. State complexity. Time and extra space, and what n is.

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.