A worked example: say it back, then name the messages
In the room, don't start typing. Start with three short moves.
1. Say the rules back, and ask what isn't said. "Each machine knows its children, not its parent. Messages arrive in any order." Then ask:
- Is it a tree, or can a machine have two parents?
- Can messages be lost or repeated?
- Is there a clock, or a timeout I can rely on?
- Does the root just need a number, or more?
Each answer is a later level in disguise. Asking shows you see them coming.
2. Write the two message kinds and the state on the board. An ask goes down, an answer comes up. Each machine keeps its parent and its answers by child. Five lines of notes, and the code almost writes itself.
3. Code the plain version, then add. Level 1 first, tested in two orders. Each follow-up is then a small change: a second value in the answer, a tick, a cache, a request id.
Here's that last point on a new question. The interviewer says: "Same tree. Now tell me the total free disk across the fleet, and which machine has the most." It's the same protocol. Each machine's answer becomes a tuple of three things computed from its own value and its children's answers:
| Part of the answer | Own value | Combined with the children by |
|---|---|---|
| machines | 1 | adding |
| total free GB | self.free_gb | adding |
| the most free, as (GB, id) | (self.free_gb, self.node_id) | max |
A pair compares its first item first, so max of (GB, id) pairs finds the most free disk and carries its id along. That works for any value that combines the same way from the bottom up.
The editor holds a level 1 Machine with free_gb, its own free disk, and an empty finish_if_done. Write finish_if_done so each answer carries the count, the total free disk and the pair for the most free.