Mock Week 3 — Hard Problems Under Time Pressure
Advertisement
Week 3 Goals
- Get a working (even brute-force) solution within 35 min
- Optimize or discuss optimization in remaining 15 min
- Practice the stuck-recovery protocol
- Keep communicating even when blocked
The Hard Problem Protocol
When you see a hard problem:
Step 1 (2 min): Brute force estimate
"I could try all subsets — that's O(2^n), too slow."
Step 2 (3 min): Pattern match
"This looks like interval DP / sliding window / union-find..."
Step 3 (5 min): Work a small example by hand
Draw the recursion tree or grid on paper.
Step 4 (5 min): State approach before coding
"I'll use a monotonic stack to track..."
Step 5 (20 min): Code the solution
Step 6 (5 min): Test + trace
Session A — DP Hard
Problem: Burst Balloons
def maxCoins(nums):
nums = [1] + nums + [1]
n = len(nums)
dp = [[0]*n for _ in range(n)]
for length in range(2, n):
for left in range(0, n-length):
right = left + length
for k in range(left+1, right):
dp[left][right] = max(
dp[left][right],
nums[left]*nums[k]*nums[right] + dp[left][k] + dp[k][right]
)
return dp[0][n-1]
Key insight: think of k as the last balloon burst in (left, right), not the first.
Session B — Graph Hard
Problem: Word Ladder II (All Shortest Paths)
Strategy: BFS to find shortest path length + build parent map. Then DFS from end to start to reconstruct all paths.
from collections import defaultdict, deque
def findLadders(beginWord, endWord, wordList):
wordSet = set(wordList)
if endWord not in wordSet: return []
parents = defaultdict(set)
layer = {beginWord}
found = False
while layer and not found:
wordSet -= layer
next_layer = set()
for word in layer:
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
nw = word[:i] + c + word[i+1:]
if nw in wordSet:
next_layer.add(nw)
parents[nw].add(word)
if nw == endWord: found = True
layer = next_layer
if not found: return []
res = []
def dfs(word, path):
if word == beginWord:
res.append(path[::-1])
return
for parent in parents[word]:
dfs(parent, path + [parent])
dfs(endWord, [endWord])
return res
The Stuck-Recovery Protocol
If you're completely blocked after 10 minutes:
- Say it out loud: "I'm thinking about this from a DP angle but I'm not sure about the state definition..."
- Ask for a hint direction: In a real interview, it's okay to say "Would it help to think about this as an interval problem?"
- Reduce the problem: "What if I only had 3 elements? What would the answer be?"
- Code brute force: Always worth coding O(2^n) or O(n²) — partial credit is real, and it often reveals the structure.
Hard Problem Patterns Cheatsheet
| Signal | Pattern |
|---|---|
| "All combinations" | Backtracking |
| "Minimum cost / maximum profit" | DP |
| "Shortest path" | BFS / Dijkstra |
| "Intervals, merging, overlapping" | Interval DP or sweep |
| "Parentheses, nesting" | Stack |
| "Subarray, contiguous" | Sliding window or Kadane |
| "Kth largest/smallest" | Heap or quickselect |
| "In a sorted array" | Binary search |
Advertisement