21/28
Part 5 of 6 · The worked exampleLearn and do

A Counter takes the set's place

3. Change the one question. Level 1 asked title not in shown. The cap asks count[title] < m, and placing a title adds 1 to its count. A Counter returns 0 for a title it hasn't seen, so there is no special case.

4. Find what the old code got for free. In level 1, shown did two jobs: the page rule and the row rule. A title placed in slot 0 was in shown, so slot 1 couldn't take it. A count doesn't do the second job. With m = 2, a row A, A, B passes count["A"] < 2 twice and shows A, A.

So the row gets its own set again, the way the grid rule needed one:

if title not in in_row and count[title] < m:
    head.append(title)
    in_row.add(title)
    count[title] += 1

This is the step to say out loud in an interview. The change looks like a one-word swap, and the bug it hides shows up only when m is 2 or more, so the level 1 tests pass.

The one-pass index still works here: a title blocked by the cap stays blocked, because counts only go up. That's the same test as before. Ask whether blocked stays blocked, and the fast scan is safe exactly when it does.

In the editor, write cap_homepage(rows, width, m): level 1, except a title is blocked once the screen shows it m times, with a set for the row.