Skip to content

Lesson 18 of 27Talk it through

The Full Walkthrough

Follow one medium problem through a complete 45-minute interview, from first read to final test, then run your own.

18 min read0/5 questions

You have practiced each stage on its own: reading, constraints, clues, brute force, plan, narration, complexity. In a real interview they happen back to back in about 45 minutes, with someone watching. This lesson puts them together on one medium problem so you can see the pacing, the words, and where the time goes.

The problem is Longest Consecutive Sequence. Read it as if you have never seen it:

Given an unsorted array of integers nums, return the length of the longest run of consecutive integers that can be formed from its values. Your algorithm must run in O(n) time.

Example: nums = [100, 4, 200, 1, 3, 2] returns 4, because 1, 2, 3, 4 are all present.

Constraints: 0 <= nums.length <= 10^5, -10^9 <= nums[i] <= 10^9

The clock

Here is how a well-paced session spends its time. The exact minutes will vary; the proportions matter.

A 45-minute session
  1. 0:00

    Read and restate

    Say the problem back. Underline 'unsorted', 'consecutive', 'O(n)'.

  2. 3:00

    Clarify

    Duplicates? Empty array? Negatives? What exactly counts as consecutive?

  3. 6:00

    Examples by hand

    Work the given example, then make an edge case of your own.

  4. 9:00

    Brute force

    Name it and its cost. Do not code it.

  5. 13:00

    Optimise and agree

    Find the repeated work, remove it, get a nod on the plan.

  6. 20:00

    Code

    About 10 minutes for 10 to 15 lines, narrating intent.

  7. 31:00

    Test

    Trace the example, then the edge cases. Fix bugs out loud.

  8. 38:00

    Complexity

    Time and space, with reasons.

  9. 41:00

    Follow-ups

    Variations, trade-offs, questions for the interviewer.

Notice that coding starts around minute 20. Nearly half the session is spent before a single line is typed, and that is normal for a strong candidate. The design minutes are where most of the signal is.

0:00 to 9:00: read, clarify, examples

Restate. "I get an unsorted list of integers. I need the length of the longest set of values that form an unbroken run, like 1, 2, 3, 4. The order in the array does not matter, only which values are present. And the target is O(n)."

Decode the clues. Three phrases do the work:

ClueWhat it implies
"unsorted"The run does not have to be next to each other in the array. Not a window, not two pointers.
"consecutive integers"For any value x, the question is whether x + 1 exists. That is a membership question.
"must run in O(n) time"Sorting (O(n log n)) is ruled out. Membership in O(1) means a hash set.

Read the constraints. 10^5 elements means O(n²) is 10^10 operations, far too slow. Values up to 10^9 rule out a counting array indexed by value.

Clarify. "Can there be duplicates, and do they count twice?" (Yes, they can appear; no, [1, 2, 2, 3] has length 3.) "Empty array returns 0?" (Yes.) "Negatives are fine, and a run can cross zero, like -1, 0, 1?" (Yes.)

Quick check
Which clarifying question changes the code the most for this problem?
Need a nudge? A hint rules out a wrong answer.

Examples. Work the given one by hand: values 100, 4, 200, 1, 3, 2. Runs: 1, 2, 3, 4 (length 4), 100 (1), 200 (1). Answer 4. Then make one of your own with an edge: [0, -1, 1, 1] gives -1, 0, 1, length 3, with a duplicate thrown in.

9:00 to 20:00: brute force, then optimise

Brute force. "For each value x, count upward: is x + 1 there, x + 2, and so on. If I check membership by scanning the list, each check is O(n), so it is O(n³) overall in the worst case. Putting the values in a set first makes each check O(1), which gets it to O(n²), because for [1, 2, ..., n] I count a long run from every starting point."

Sorting. "Sorting and scanning for runs is O(n log n). That is a clean answer and I would take it if O(n) were not required. But the prompt says O(n), so I need to avoid sorting."

Find the repeated work. "In the O(n²) version, starting from 2 recounts the run I already counted from 1. Starting from 3 recounts it again. I only need to count each run once, from its first value. A value x is the start of a run exactly when x − 1 is not in the set."

Agree the plan.

  1. Put every value in a set (this also drops duplicates).
  2. For each value x in the set, skip it if x − 1 is in the set.
  3. Otherwise count up from x while the next value exists.
  4. Track the longest count.

"Each value is counted at most once inside a run, so the total is O(n). Does that sound good before I write it?"

Only start counting at the start of a run
100
0
↑x
4
1
200
2
1
3
3
4
2
5
best = 1

x = 100. Is 99 in the set? No, so 100 starts a run. Is 101 there? No. Run length 1.

1/5
Quick check
There is a while loop inside a for loop. Why is the algorithm still O(n)?
Need a nudge? A hint rules out a wrong answer.

20:00 to 31:00: code

Narrate the blocks, not the keystrokes: "First the set. Then the loop, with the skip check. Then the count. Then I update the best."

Pythonlongest_consecutive.py
def longest_consecutive(nums: list[int]) -> int: seen = set(nums) best = 0 for x in seen: if x - 1 in seen: continue # not the start of a run length = 1 while x + length in seen: length += 1 best = max(best, length) return best print(longest_consecutive([100, 4, 200, 1, 3, 2]))print(longest_consecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]))print(longest_consecutive([]))print(longest_consecutive([0, -1, 1, 1]))
Output
Press Run (⌘ Enter) to see what happens.

Two small choices worth saying out loud: "I loop over the set, not the original list, so duplicates are not processed twice." And: "I start best at 0 so the empty array returns 0 without a special case."

31:00 to 38:00: test

Testing is a separate stage, not a glance. Trace the main example step by step (the visual above is exactly that trace), then run through edge cases and say what each one proves.

Match the pairs0/5 matched

Match each test input to what it checks.

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

What does this print?

print(longest_consecutive([10, 5, 12, 3, 11, 4]))
Need a nudge? Hints walk you through it.

38:00 to 45:00: complexity and follow-ups

"O(n) time: building the set is O(n), and the loop does O(1) work per value plus run walks that total at most n. O(n) extra space for the set."

Then the interviewer may stretch it:

  • "What if O(n log n) were allowed?" "Sort and scan, skipping duplicates. O(1) extra space if I sort in place, which is a real trade-off when memory is tight."
  • "What if values arrive one at a time?" "I would keep a map from each run's endpoints to its length and merge neighbours when a new value lands. Union-find also works."
  • "Can you return the run itself?" "Keep the start value along with best, then return range(start, start + best)."
Put it in order6 steps

Put the stages of this walkthrough in order.

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

Your turn

Do the full walkthrough yourself. Set a 45-minute timer, open the Decoder on a medium problem you have not solved, and work through every stage out loud before you look at the model answers. Then code it below.

First, type Longest Consecutive Sequence from memory, narrating as you go:

ChallengeMedium

Longest Consecutive Sequence

Given an unsorted list of integers nums, return the length of the longest run of consecutive values (like 4, 5, 6, 7) that can be formed from its elements. The values do not need to be next to each other in the list. Your solution must run in O(n) time.

nums = [100, 4, 200, 1, 3, 2]          →  4   (1, 2, 3, 4)
nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]  →  9   (0 through 8)
nums = []                              →  0

Duplicates may appear and count once.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
def longest_consecutive(nums: list[int]) -> int: # Only start counting from a number that begins a run. pass

Then a fresh one, start to finish, using the same clock:

ChallengeMedium

Group Anagrams

Given a list of words strs, put words that are anagrams of each other (same letters, same counts) into the same group. Return the list of groups. The order of the groups, and the order inside each group, does not matter.

["eat", "tea", "tan", "ate", "nat", "bat"]
  →  [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
 
[""]   →  [[""]]
["a"]  →  [["a"]]

Constraints

  • 0 <= strs.length <= 10^4
  • 0 <= strs[i].length <= 100
  • strs[i] contains only lowercase English letters.
def group_anagrams(strs: list[str]) -> list[list[str]]: # Give every word a key that anagrams share. pass
Key terms
pacing
Splitting the session so design gets about as much time as coding. Coding often starts near minute 20.
run start
A value x whose predecessor x − 1 is absent. Counting only from starts makes the scan O(n).
membership question
A step that asks 'is this value present?'. A hash set answers it in O(1) on average.
follow-up
A variation the interviewer adds at the end, such as streaming input or a tighter memory limit.
verification
Tracing your own code on an example and on chosen edge cases, saying what each one tests.