iq.lab
Python starts when a code cell comes near or you run one
14 Interview mode

Lesson 2 of 5

Running the live round

A coding round has a script of five phases (clarify, plan, implement, test, summarize), each with its minutes. Watch one medium problem solved out loud with each phase tied to the score it feeds, learn what to say when you are stuck, and answer the five follow-ups before they are asked.

About 1 hour
By the end you can
  • Run the five phases of the think-aloud script within their minutes, in the one-problem and two-problem formats

  • Turn a vague prompt into an exact one with clarifying questions whose answers change the code

  • Narrate decisions while you code, and test by tracing a typical case and the edge cases aloud

  • Recover when stuck by saying where you are, working a small example and using a hint in your own words

  • Answer the five follow-up types for a sliding window problem

The interviewer pastes a prompt into a shared editor, reads it aloud, and then watches you work for 45 minutes. Afterwards they write up what they saw and heard. At Google, a committee that never met you decides from that write-up. A good idea that stays in your head never reaches it.

Talking does not replace correct code. In platform data from about 100,000 interviews, candidates strong on code and problem solving but weak on communication advanced 96% of the time. Candidates a step weaker on code and problem solving but strong on communication were rejected about three times more often. The script keeps the code at the center and does the work in an order the interviewer can follow.

This lesson runs the script on one medium problem, phase by phase, then covers getting stuck and the five follow-up questions that come after a working answer.

The script and its clock

What do you do in each minute? The research digest gives a think-aloud script with five phases. Module 0 drew them on a timeline. Here they are with the work each phase produces and the score it feeds most. The four scores on the interviewer's rubric (their scoring guide) are communication, problem solving, technical competency (correct, readable code) and testing.

PhaseMinutesWhat you doScore it feeds most
Clarify2 to 3Confirm the input: can it be empty, can values repeat, how large is n. Restate the problem in one sentence.Communication
Plan3 to 4Name a brute force (the plain approach that tries every possibility) and its cost, then a better approach and its cost. Say which you will write and why. State the promise (what stays true after every step) before you type.Problem solving
Implement12 to 15Write the code. Narrate decisions, not keystrokes.Technical competency
Test5 to 7Trace a typical case line by line, then empty input, then duplicates or boundaries. Announce a bug before you fix it.Testing
Summarize2State time and space. Offer the follow-up you expect.Problem solving

The five phases add up to 24 to 31 minutes, in line with the digest's bar for a mid-level (L4) generalist: an unseen medium solved, tested and narrated in roughly 20 to 30 minutes, with the cost stated and the edge cases (unusual inputs, such as an empty list) raised before anyone asks. The rest of the 45 goes to the hellos, the follow-ups and your own questions. Communication is not only the clarify phase: you feed it every time you speak.

Meta's format tests speed: two mediums in about 35 to 40 minutes of coding inside the 45, often with no code execution, while the interviewer watches you test (reported and candidate-reported). The timeline below shows that format, each block as wide as its minutes. Select a block to read what to do in it and a sentence to say.

Even the short end of the script, 24 minutes, does not fit in a 20-minute block. So at speed every phase shrinks. The question is what to keep.

Quick checkTwenty minutes for a whole problem

In the two-mediums format each problem gets about 20 minutes, and the full script takes at least 24. Which plan for one 20-minute block keeps the most of the four scores?

Choose one answer, then check.

Clarify and plan, out loud

How does a vague prompt become an exact one? Here is a medium problem, run through the script from minute 0. The interviewer reads:

"Our monitor checks a service once a minute and logs 1 if it answered and 0 if it did not. Find the longest stretch of uptime if we forgive up to k missed checks."

(Its LeetCode twin is 1004, Max Consecutive Ones III, and module 4's checkpoint solved it. This time the point is how to run it out loud.) Clarify: ask the questions whose answers change the code. The fine print from the last lesson (sorted or not, negatives or not, positions or values) is made of questions like these. Here is the exchange, with what each answer decides:

You askThe interviewer saysWhat it decides
"Is the log a list of 0s and 1s, in time order?""Yes."What one item is.
"Do you want the length of the stretch, or where it is?""The length, in checks."What to return.
"Is a stretch consecutive checks, and does a forgiven miss count toward its length?""Yes to both."The rule.
"Can k be 0? Can the log be empty?""Both. An empty log gives 0."Two edge cases, raised before anyone asks.
"How long can the log get?""Up to 105 checks."The cost you can afford.

Then restate the problem in one sentence: "So I return the length of the longest run of consecutive checks that holds at most k zeros." If the restatement is wrong, the interviewer corrects it at minute 3, not at minute 25. All of this feeds communication: you asked before you assumed. At Google, prompts can be vague, and then these questions are part of the score (reported).

The last answer sets the budget. Python does about 107 simple steps per second (module 2's rule of thumb). Trying every start with every end on 105 checks means up to 105 × (105 + 1) / 2 pairs, about 5 × 109 of them: at 107 a second that is 500 seconds, more than 8 minutes. So the plan needs O(n) or O(n log n).

Practice it on a prompt that is vaguer still.

Say itClarify a vague prompt

The interviewer says: "Here's our login log. Find the users who log in the most." Write the clarifying questions you would ask, then restate the problem in one sentence, assuming reasonable answers.

A few sentences first: 0 of 60 characters.

Saved in this browser as you type.

Plan: the slow way and its cost, the better way and its cost, then the promise. Here is the plan phase as you would say it. It feeds problem solving:

"The brute force tries every first check and extends right until the stretch holds more than k misses: up to about n2/2 start and end pairs, 5 × 109 for 105 checks. Too slow. Better: a sliding window. The right end takes one more check each step; when the window holds more than k misses, the left end moves right until it holds at most k. Each check enters once and leaves at most once: O(n) time, O(1) extra space. The promise: after each step, the window is the longest good stretch ending at the right end, and best is the longest seen so far. I'll write the window."

That is the variable window from module 4: move right every step, and while the window breaks the rule, move left. At Google, writing the brute force first (10 to 15 minutes) and then the faster version is reported as standard. Either way, say the brute force's cost before you improve on it.

Implement, test and summarize, out loud

What do you say while you type? Narrate decisions, not keystrokes. Reading a line aloud tells the interviewer nothing the screen does not; saying why it is there tells them what the screen cannot. The code you type is in the trace further down. For its four key lines, here is the difference:

Instead of reading the lineSay the decision
"for right in range len checks""I loop over positions, because the window's length needs indexes."
"if checks right equals zero, misses plus one""I count the misses inside the window, so the rule is one comparison."
"while misses greater than k""While the window breaks the rule, I move left. After this loop it holds at most k misses."
"best equals max of best and right minus left plus one""I measure only after the while loop, when the window is good again."

The code feeds technical competency; the reasons you give feed communication.

Then test. Pick a typical case and say its expected answer before you trace. For [1, 0, 1, 1, 0, 1] with k = 1, checks 0 to 3 hold one miss and so do checks 2 to 5, both 4 long, while every stretch of 5 or 6 checks holds both misses. So you expect 4. The trace below is that test. Before you step through, guess where left ends up.

Step throughTesting out loud: a typical case
Step 1 of 41
Line 1 is about to run.
def longest_uptime(checks, k):
left = 0
misses = 0
best = 0
for right in range(len(checks)):
if checks[right] == 0:
misses += 1
while misses > k:
if checks[left] == 0:
misses -= 1
left += 1
best = max(best, right - left + 1)
return best
Names
no names yet
Output
(nothing printed yet)

Out loud, the hard step sounds like this: "At right 4 a second miss joins, one too many. Left drops check 0, a success, then check 1, the miss. Now left is 2." Then check the length formula, where off-by-one bugs (one too many or one too few) hide: "Left 2 and right 5 cover checks 2, 3, 4 and 5, four checks, and 5 − 2 + 1 is 4."

Saying the expected answer first is what catches a bug. Had you typed right - left, this same trace would return 3. Announce the bug before you touch the code: "I expected 4 and got 3. Left 2 to right 5 is four checks, so my length is one short: it needs the + 1." This is the work the testing score rewards, and the next lesson turns it into a full procedure for rounds with no run button.

Put in orderPut the window back together

Put the lines of longest_uptime(checks, k) in order. The three starting values are set on one line. Two of the lines do not belong.

Your program

Add lines from the list below, in order.

    Unused lines (some do not belong)
    • misses -= 1
    • for right in range(len(checks)):
    • for left in range(len(checks)):
    • def longest_uptime(checks, k):
    • if checks[left] == 0:
    • left, misses, best = 0, 0, 0
    • return best
    • misses += 1
    • if checks[right] == 0:
    • while misses > k:
    • best = max(best, right - left)
    • left += 1
    • best = max(best, right - left + 1)

    A typical case catches the glaring bugs; the edge cases catch the quiet ones. The clarify phase named two: an empty log and k = 0. Add the opposite of k = 0, a k bigger than the number of misses.

    PredictThe edge cases, before you say done

    The test phase goes on to the edge cases. Type exactly what this prints, one value per line.

    def longest_uptime(checks, k):
        left = 0
        misses = 0
        best = 0
        for right in range(len(checks)):
            if checks[right] == 0:
                misses += 1
            while misses > k:
                if checks[left] == 0:
                    misses -= 1
                left += 1
            best = max(best, right - left + 1)
        return best
    
    print(longest_uptime([], 2))
    print(longest_uptime([0, 0, 0], 0))
    print(longest_uptime([0, 0, 0], 5))
    print(longest_uptime([1, 1, 0, 1], 0))
    

    Summarize: time and space in one sentence, then offer the follow-up you expect. "One pass. Each check enters the window once and leaves at most once, so O(n) time and O(1) extra space. I can also talk about running this on a live stream." A while loop inside a for loop looks like O(n2), and the interviewer may ask why it is not. left only moves right and never passes right + 1, so across the whole call the line left += 1 runs at most n times, not n times per pass.

    Work it outHow far does left travel?

    Work through longest_uptime([0, 1, 0, 0, 1, 1], 1) on paper. How many times, in total over the whole call, does the line left += 1 run? Type a whole number.

    times left += 1 runs

    Type a number: 12, -1, 0.5 and 3/4 all work. Enter checks.

    Recap: the whole round on one page, with the score each phase fed:

    PhaseMinutesWhat the interviewer sawScore
    Clarify0 to 3Questions about the input, the output and the size, and a one-sentence restatementCommunication
    Plan3 to 7The brute force and its cost, the window and its cost, the promiseProblem solving
    Implement7 to 19The window code, with each decision said aloudTechnical competency
    Test19 to 25A typical case with its expected answer, the length formula checked, then the edge casesTesting
    Summarize25 to 27O(n) time, O(1) space, and a follow-up offeredProblem solving

    Twenty-seven minutes, all four scores fed, and 18 minutes left for the hellos, the follow-ups and your questions.

    When you get stuck

    What if the window does not come to you? Rewind to minute 5. You have the brute force and nothing better. A long silence hides your thinking from the person scoring it. The digest's stuck recovery script has five moves:

    1. Say where you are within about a minute of silence.
    2. Drop to a concrete example and work it by hand.
    3. Say what you have ruled out, and why.
    4. Restate a hint in your own words, then use it.
    5. Announce a pivot cleanly when you change approach.

    Say where you are, as a frame. "I have a brute force: every start, extending until there are too many misses. It's O(n2). I'm looking for a way not to start over at every start." Its frame shows the interviewer exactly what you have:

    Then work a small example. The trace runs the brute force on the same log. Watch the two arrows. Before you step through, guess: when start moves from 0 to 1, where does end go?

    Step throughStuck: the brute force on a small log
    Step 1 of 105
    Line 1 is about to run.
    def longest_uptime_slow(checks, k):
    best = 0
    for start in range(len(checks)):
    misses = 0
    for end in range(start, len(checks)):
    if checks[end] == 0:
    misses += 1
    if misses > k:
    break
    best = max(best, end - start + 1)
    return best
    Names
    no names yet
    Output
    (nothing printed yet)

    That re-reading is the repeated work. Say so: "Each start re-reads checks the last start already read." Then say what you ruled out: "Sorting is out. The stretch must be consecutive, and sorting destroys the order." Then the interviewer offers a hint: "When start moves one check right, can a stretch that was good become bad?"

    Restate the hint in your own words, then use it: "No. Moving start right drops a check from the front, which can only remove a miss. So every end the last start reached is still good, and end never goes back. Both ends only move right: a sliding window, O(n)." Then announce the pivot: "I'll replace the nested loops with a window." The restatement shows you understood the hint and turns it into your plan.

    Quick checkTaking a hint

    A different round. You are counting the pairs of numbers that add up to a target, and you have a nested loop, O(n2). The interviewer says: "What if you remembered the numbers you have already passed?" Which response uses the hint best?

    Choose one answer, then check.

    Recap: when you are stuck, keep talking in five moves. Say what you have, work a small case, say what is ruled out, restate any hint as your own plan, and announce the pivot.

    Five follow-ups on one problem

    What happens after "done"? Follow-ups are predictable in shape, and there are five shapes. Here they are for the uptime problem, each with a model answer:

    TypeThe interviewer asksA model answer
    Complexity probe"There's a while loop inside the for loop. Isn't that O(n2)?""No. Left only moves right, at most n times over the whole call. With right's n steps, that's at most 2n moves: O(n) time, O(1) space."
    Scale shift"The log is a live stream. You can't store it.""Right reads each check once, so it works on a stream. Only left looks back, at checks[left]. I'd keep the positions of the misses inside the window instead, at most k + 1 of them: O(k) memory."
    Constraint flip"Each check now logs seconds of downtime, 0 to 60. Forgive up to M seconds in total.""Downtime is never negative, so adding a check never lowers the window's total and dropping one never raises it. The same window works, with a running sum of downtime instead of a miss count."
    Extension"Return where the stretch starts and ends.""Whenever the window beats best, also save left and right."
    Rationale probe"Why a sliding window and not the nested loops?""Growing a stretch can only add misses. So once left to right holds more than k, every stretch that starts at left or earlier and ends at right or later does too. Those starts are finished, left never moves back, and one pass is enough: O(n) instead of O(n2)."

    The scale answer changes the notebook, so watch it run. The log is the same and so is the answer, but now nothing reads checks[left], so no old check has to be kept: the walk takes each check once, in order, which is all a stream allows. A deque (module 5: a list with cheap adds and removals at both ends) holds the positions of the misses inside the window. Before you step through, guess the most positions it ever holds at once.

    Step throughThe scale follow-up: a stream with O(k) memory
    Step 1 of 36
    Line 1 is about to run.
    from collections import deque
    def longest_uptime_stream(checks, k):
    missed_at = deque()
    left = 0
    best = 0
    for right, check in enumerate(checks):
    if check == 0:
    missed_at.append(right)
    if len(missed_at) > k:
    left = missed_at.popleft() + 1
    best = max(best, right - left + 1)
    return best
    Names
    no names yet
    Output
    (nothing printed yet)

    The extension is the shortest change of all, and it is yours to write.

    Code itThe extension: where is the stretch?

    The extension follow-up. Write uptime_window(checks, k), which returns (first, last): the positions of the first and last check of the longest stretch with at most k misses. If several stretches tie, return the earliest. If no check can be in a stretch (an empty log, or only misses with k = 0), return None. Then press Watch it run.

    ⌘+Enter runsPython starts when a code cell comes near or you run one
    Write your code where the starter says raise NotImplementedError, then press Run tests. Each check says what it expects.

    Last, answer a scale follow-up for another problem: the login log you clarified earlier.

    Say itAnswer a scale follow-up

    Back to the login log from the clarify exercise. Suppose you solved it with a Counter of logins per user and a heap that picks the top k. The interviewer asks: "The log is far bigger than one machine's memory. What changes?" Write what you would say.

    A few sentences first: 0 of 60 characters.

    Saved in this browser as you type.

    Next: Testing without a run button