Skip to content

Lesson 14 of 27Talk it through

Getting Unstuck

Use eight concrete moves to break out of a stuck moment, and take interviewer hints without losing momentum.

14 min read0/5 questions

Every strong engineer gets stuck in interviews. The difference is that they have a routine for it. Interviewers expect you to stall at some point on a medium or hard problem, and much of what they score is what you do next: freeze, or work the problem.

This lesson is that routine: a toolbox of moves, each with a sentence you can say while you use it.

First, say where you are stuck

"I am stuck" gives nobody anything to work with. "I am stuck" plus where turns it into a problem you and the interviewer can solve together.

  • "I have an O(n²) version. The slow part is the inner loop that searches for a match."
  • "I can solve it if the array is sorted, but sorting loses the original order, which I need."
  • "I do not see how to handle the case where the window has to shrink."

Naming the block often unblocks you by itself, because a precise question usually points at its own answer. "The slow part is a search" already suggests a hash map or binary search.

Quick check
Which statement gives an interviewer the most to work with?
Need a nudge? A hint rules out a wrong answer.

The toolbox

Try these roughly in this order. The first three are cheap and almost always help.

1. Use a smaller example

Big examples hide the structure. Shrink to three or four elements and solve it by hand, slowly, writing down what you look at. The steps your hand takes are often the algorithm.

2. Write the brute force

If you cannot see the clever version, write (or at least describe) the obvious one. It gives you a correct baseline, a test oracle, and a clear target: which part of it repeats work?

3. Draw it

Arrays with pointers, trees with arrows, a grid with the cells you visited. Drawing turns "I think it moves like this" into something you can check. Here is Product of Array Except Self drawn out. The brute force multiplies everything except i for each i. Drawing it shows each answer is "everything to the left" times "everything to the right".

Draw it: left products times right products
1
0
2
1
3
2
4
3

We want the product of everything except index 1. Drawn out, that is the part to the left (1) times the part to the right (3 × 4).

1/4

4. Change the data structure

Look at what the slow part does. Is it a lookup ("have I seen this?")? Use a hash map or set. Is it "the smallest so far" or "the k largest"? Use a heap. Is it "the nearest bigger one to the left"? Use a monotonic stack. Most optimisations are a data structure swap.

5. Sort first

Sorting costs O(n log n) and buys a lot: duplicates sit next to each other, two pointers work, intervals line up, greedy choices become obvious. If the constraints allow O(n log n) and order does not matter for the answer, ask "what if it were sorted?"

6. Think in reverse

Go right to left, work backwards from the goal, or flip the question. "For each day, how long until a warmer one?" is easier from the right, keeping a stack of candidates. "Which cells can reach the ocean?" is easier as "which cells can the ocean reach?" Counting what you cannot do is sometimes easier than counting what you can.

7. Solve a simpler version

Remove one rule and solve that. No duplicates. Only positive numbers. k = 1. One row instead of a grid. Then add the rule back and ask what breaks. For 3Sum, the simpler version is Two Sum on a sorted array, and 3Sum is that inside a loop.

8. Read the constraints again

The constraints are a hint the problem gives you for free. n ≤ 20 says exponential search is fine. n ≤ 10^5 says you need O(n log n) or better. "Only lowercase letters" says a 26-slot count array works. "Sorted" says binary search or two pointers. If you skipped them, go back.

Match the pairs0/5 matched

Match each stuck moment to the move most likely to help.

Choose an item, then its match on the other side.

The brute force as a safety net

When you are stuck on the fast version, a brute force is not wasted time. It lets you test any idea on dozens of inputs in seconds. This is a habit worth keeping long after the interview.

Pythonoracle.py
import random def brute(nums: list[int]) -> list[int]: out = [] for i in range(len(nums)): p = 1 for j, x in enumerate(nums): if j != i: p *= x out.append(p) return out def fast(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out random.seed(7)for _ in range(300): nums = [random.randint(-3, 3) for _ in range(random.randint(2, 6))] assert fast(nums) == brute(nums), numsprint("300 random tests agree")
Output
Press Run (⌘ Enter) to see what happens.

Try breaking fast (for example, start right at 0) and run it again. The assert prints the exact input that fails, which is your next small example.

Predict the outputPython

What does this print?

nums = [2, 3, 4]
out = [1] * len(nums)
left = 1
for i in range(len(nums)):
  out[i] = left
  left *= nums[i]
print(out)
Need a nudge? Hints walk you through it.

Taking hints well

A hint is not a penalty. Interviewers give hints to keep the session productive, and most rubrics care far more about how you use one than whether you needed it. A good response has four parts:

  1. Listen fully. Do not start talking over it.
  2. Repeat it in your words. "So you are suggesting I think about the sorted case."
  3. Connect it to your block. "If it is sorted, then equal values are adjacent, so I can skip duplicates with a pointer."
  4. Act on it. Update the plan out loud and keep moving.

What hurts you: ignoring the hint and continuing down your path, arguing that your way also works when it clearly does not, or needing the same hint twice.

Quick check
You are stuck on an O(n²) solution. The interviewer asks: 'What if you processed the array from the right?' What is the strongest reply?
Need a nudge? A hint rules out a wrong answer.
Put it in order5 steps

Put this unstuck routine in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

If you want a structured way to practice this, open the Decoder on a problem you have never seen and stop at the plan stage. Before revealing it, try two moves from the toolbox and say which one helped.

ChallengeMedium

Product of Array Except Self

Given a list of integers nums, return a list answer where answer[i] is the product of every number except nums[i]. Do it in O(n) time without using division.

nums = [1, 2, 3, 4]     →  [24, 12, 8, 6]
nums = [2, 3]           →  [3, 2]
nums = [1, 2, 0, 4]     →  [0, 0, 8, 0]

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • Every prefix and suffix product fits in a 32-bit integer.
def product_except_self(nums: list[int]) -> list[int]: # Everything to the left times everything to the right. pass
ChallengeMedium

Daily Temperatures

Given a list of daily temperatures, return a list answer where answer[i] is how many days you have to wait after day i for a strictly warmer day. If no warmer day comes, answer[i] is 0.

[73, 74, 75, 71, 69, 72, 76, 73]  →  [1, 1, 4, 2, 1, 1, 0, 0]
[30, 40, 50, 60]                  →  [1, 1, 1, 0]
[60, 50, 40]                      →  [0, 0, 0]

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100
def daily_temperatures(temperatures: list[int]) -> list[int]: # Keep a stack of days still waiting for a warmer day. pass
Key terms
test oracle
A slow but obviously correct solution used to check a faster one on many inputs.
simpler version
The same problem with one rule removed, solved first so the full version becomes a small change.
think in reverse
Scanning from the other end or working back from the goal when the forward view needs future information.
repeated work
The part of a brute force that recomputes something it already knew. Removing it is usually the optimisation.
taking a hint
Listen, repeat it in your words, connect it to your block, act on it.