The 6 step method (say it out loud)
Interviewers grade how you think as much as the code. Narrate everything.
| 1. Repeat | "So I get X and return Y." Confirms you understood. |
| 2. Clarify | Empty input? Duplicates? Negatives? Sorted? Size of n? Can I modify the input? |
| 3. Example | Walk one small example by hand, plus one edge case. |
| 4. Brute force | Say it with its Big O. Don't code it unless asked. |
| 5. Improve, then code | Name the pattern ("hash map", "two pointers"). Code it clean, with good names. |
| 6. Test | Run your example through the code line by line. Then edge cases. Then state time and space. |
Which pattern? Look at the input
| If you see | Try |
| "Have I seen this?", counts, pairs | Map or Set |
| Sorted array, palindrome, in-place rewrite | Two pointers |
| Best substring or subarray | Sliding window |
| Matching pairs, nesting, "most recent" | Stack |
| Sorted plus find, or "minimum X that works" | Binary search |
| Tree depth or path | Recursion (DFS) |
| Level by level, fewest steps | BFS with a queue |
| Grid, connected areas | DFS or BFS flood fill |
| Ranges, meetings, schedules | Sort by start, then merge |
| "How many ways", best value from smaller cases | DP |
When stuck
- "Let me try a smaller example." Then do it.
- "I'll start with the brute force so we have something correct, then optimise."
- Ask: "Am I on the right track?" Interviewers usually nudge.