π― Coding Interview Best Practices
The meta-game of interviews. Technical skill + communication = success.
TL;DR: The 45-Minute Frameworkβ
| Phase | Time | What to Do | Red Flags |
|---|---|---|---|
| Understand | 5 min | Clarify, examples, edge cases | Jumping to code |
| Plan | 5 min | Pattern, approach, complexity | No plan stated |
| Code | 20-25 min | Clean, modular, talking | Silent coding |
| Test | 8-10 min | Walk through, edge cases | "I think it works" |
| Optimize | 2 min | Time/space, trade-offs | Forgot complexity |
The UMPIRE Methodβ
A battle-tested framework used by Google engineers.
U - Understand the Problemβ
Before anything else, make sure you understand WHAT to solve.
β
DO:
- Repeat the problem in your own words
- Ask about input/output format
- Clarify constraints (size, range, types)
- Ask for examples
- Identify edge cases
β DON'T:
- Assume anything
- Start coding immediately
- Say "I've seen this before"
Questions to ask:
- "What's the input format?"
- "What should I return if the input is empty?"
- "Are there duplicates? How should I handle them?"
- "Can I modify the input array?"
- "What's the expected size of the input?"
M - Match to Patternsβ
What type of problem is this?
Say it out loud:
"This looks like a sliding window problem because we're looking for a subarray with a certain property."
P - Plan the Approachβ
Before coding, state your plan clearly.
β
Good Planning:
"I'll use a HashMap to track character frequencies.
I'll iterate through the string once, updating counts.
Time: O(n), Space: O(k) where k is unique characters."
β Bad Planning:
"Let me just start coding and see..."
Get buy-in from interviewer:
"Does this approach sound reasonable before I start coding?"
I - Implementβ
Now write clean, modular code.
Best Practices:
# β
GOOD: Clear variable names, comments
def find_longest_substring(s: str) -> int:
char_index = {} # Store last seen index of each char
max_length = 0
start = 0
for end, char in enumerate(s):
# If char seen and within current window
if char in char_index and char_index[char] >= start:
start = char_index[char] + 1
char_index[char] = end
max_length = max(max_length, end - start + 1)
return max_length
# β BAD: Unclear names, no structure
def f(s):
d = {}
m = 0
st = 0
for i, c in enumerate(s):
if c in d and d[c] >= st:
st = d[c] + 1
d[c] = i
m = max(m, i - st + 1)
return m
While coding:
- Talk through your logic
- Write helper functions for complex parts
- Don't worry about perfect syntax
- If unsure about API, say so: "I think the syntax is X, let me know if I'm wrong"
R - Review and Testβ
Never say "I think it works." PROVE IT.
Testing Strategy:
-
Walk through with given example
Input: [1, 2, 3], target = 5
Step 1: i=0, current=1, complement=4, not in map
Step 2: i=1, current=2, complement=3, not in map
Step 3: i=2, current=3, complement=2, FOUND at index 1
Return: [1, 2] β -
Test edge cases
- Empty input:
[] - Single element:
[5] - No solution exists
- All same elements:
[1, 1, 1, 1]
- Empty input:
-
Test boundaries
- Maximum values
- Negative numbers
- Very long strings
E - Evaluate Complexityβ
Always state time and space complexity.
β
GOOD:
"Time complexity is O(n) because we iterate once.
Space is O(k) for the HashMap, where k is unique characters.
In the worst case, k = n, so O(n) space."
β BAD:
"It's pretty efficient."
Communication: The Hidden Skillβ
Talk Out Loud (Always)β
Interviewers assess your thinking, not just the code.
| Instead of... | Say... |
|---|---|
| Silent thinking | "I'm thinking about whether to use DFS or BFS here..." |
| "Let me think" | "Let me trace through an example to verify my logic..." |
| Getting stuck silently | "I'm stuck on X, let me try approach Y..." |
How to Handle Being Stuckβ
Script for being stuck:
"I'm stuck on the optimal solution. Let me start with a brute force approachβthat would be O(nΒ²) by checking every pair. Now let me think about how to optimize...
Actually, if I sort the array first, I could use two pointers... that would be O(n log n) for sort plus O(n) for the search."
Handling Hints Gracefullyβ
β Good response to hints:
"Ah, that's a great hint! So if I use a HashMap here, I can reduce the lookup from O(n) to O(1). Let me refactor..."
β Bad response:
"Oh, I knew that already, I was just about to do that."
Code Quality Checklistβ
Before You Startβ
- Understood the problem?
- Clarified edge cases?
- Stated approach and complexity?
- Got interviewer's buy-in?
While Codingβ
- Clear variable names?
- Talking through logic?
- Handling edge cases?
- Modular (helper functions)?
After Codingβ
- Walked through with example?
- Tested edge cases?
- Stated time complexity?
- Stated space complexity?
- Discussed trade-offs?
Common Mistakes (And How to Avoid Them)β
β Mistake 1: Jumping Into Codeβ
Problem: You start coding without understanding the problem.
Fix: Force yourself to repeat the problem and ask at least 3 clarifying questions.
β Mistake 2: Going Silentβ
Problem: You think quietly for 2+ minutes.
Fix: Narrate your thinking: "I'm considering using a hash map because..."
β Mistake 3: Not Testingβ
Problem: "I think it works" without verification.
Fix: Always trace through your code with at least one example.
β Mistake 4: Over-Engineeringβ
Problem: Adding fancy optimizations before basic solution works.
Fix: Get a working solution first, then optimize.
β Mistake 5: Getting Defensiveβ
Problem: Arguing when interviewer points out bugs.
Fix: "Good catch! Let me fix that." Then debug systematically.
Real Interview Walkthroughβ
Problem: Find two numbers that add to target.
Phase 1: Understand (2 min)β
Me: "So I need to find two numbers in the array that sum to target. Should I return indices or values?"
Interviewer: "Indices."
Me: "Is the array sorted? Are there duplicates?"
Interviewer: "Not sorted, can have duplicates."
Me: "And exactly one solution exists?"
Interviewer: "Yes, assume valid input."
Phase 2: Plan (2 min)β
Me: "Brute force would check every pairβO(nΒ²). But I can use a HashMap to store complements. For each number, check if target - num exists. That's O(n) time, O(n) space. Should I proceed?"
Interviewer: "Sounds good."
Phase 3: Code (8 min)β
def two_sum(nums: list[int], target: int) -> list[int]:
# Map: value -> index
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] # No solution (shouldn't happen per constraints)
Phase 4: Test (3 min)β
Me: "Let me trace through: nums = [2, 7, 11], target = 9
- i=0, num=2, complement=7, not in seen, add
{2: 0}- i=1, num=7, complement=2, 2 is in seen! Return [0, 1]
Let me also check empty array: returns [] which is correct."
Phase 5: Complexity (1 min)β
Me: "Time O(n) - single pass. Space O(n) - HashMap in worst case stores n elements."
Interview Day Tipsβ
Before the Interviewβ
- Get 7+ hours of sleep
- Eat a light meal (not hungry, not sluggish)
- Review your top 5 patterns
- Test your setup (camera, mic, IDE)
- Have water nearby
During the Interviewβ
- Take a breath before answering
- It's OK to ask for a moment to think
- Ask clarifying questions
- Start with brute force if stuck
- Test your solution
After Mistakesβ
β "Oh no, I'm so dumb"
β "This is easy, I can't believe I missed that"
β Arguing with interviewer
β
"Good catch, let me fix that"
β
"I see the bugβit's in the loop condition"
β
"Thanks for the hint, that helps"
Key Takeawaysβ
- Never code without a plan
- Talk out loud the entire time
- Start simple, then optimize
- Test with real examples
- Handle mistakes gracefully
- State complexity at the end
Next: Two Pointers Pattern β - Your first pattern.