54Count the machines in a tree

mediumpython
60:00

54 Count the machines in a tree

OpenAI software engineer candidates report a coding question about counting the machines in a tree (2 reports). In the PracHub write-up of q-oai-092, a technical screen, you get a root node and one call, sendAsyncMessage(nodeId, message), and you write receiveMessage(fromNodeId, message) yourself. Each node is a separate machine, so the only way to reach a child is a message. The write-up lists the follow-ups: print the whole tree's structure, handle a machine that crashes or a message that fails, with retries, a request_id and cached answers so nothing is counted twice, keep the state of different requests apart, and build sendAsyncMessage itself and test it. q-oai-131 names the question, "counting machines", and gives no detail. This is practice in that shape, not a leaked question.

What we designed: the network below, the message order and loss rules the tests use, the tick() timer, the tree format, and machines that join between counts. The reports don't give those details.

Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/54_machine_count -k level1. Earlier levels must keep passing.

The network. The tests own it, and it runs in plain Python: no threads and no sleeps. Every machine is a Machine(node_id, children, send). children is the list of its children's ids. A machine knows nothing else about the tree: not its parent, not the root, not the size. send(to_id, message) is sendAsyncMessage: it puts the message in the network's queue and returns at once. Later the network calls receive(from_id, message) on the machine it was sent to. That's receiveMessage. A message can be any Python value you choose, such as a tuple. Messages arrive in any order: the tests deliver them first in, first out, then last in, first out, then shuffled with several seeds. Two messages on the same link can arrive swapped.

Level 1. Count the machines. The test calls start() on the root, then delivers messages until none are left. By then the root must have set self.count to the number of machines in the tree, itself included. Until it knows the answer, self.count stays None. A root with no children counts 1 and sends nothing.

Level 2. The tree's shape. When the count finishes, the root also sets self.tree to the whole tree as a string: a machine's id, then its children's shapes in parentheses, separated by commas, in the order of its children list. A machine with no children is just its id. The tree 0 -> [1, 2], 1 -> [3, 4] is "0(1(3,4),2)", whatever order the answers arrived in.

Level 3. Lost and repeated messages. The network may now lose a message, and it may deliver one twice. When the queue runs dry and the root has no answer, the test calls tick() on every machine, the way a timeout would fire. On a tick, a machine that's still waiting asks again, but only the children that haven't answered yet. A machine asked again after it finished sends the same answer again, without asking its children again. A machine asked again while it still waits does nothing: its answer goes up when it finishes. An answer that arrives twice counts once. A finished count sends nothing on a tick.

Level 4. Several counts. start(request_id=None) now takes an id, and the root may start a new count while another is running, so messages of different counts mix. When a count finishes, the root stores it in self.counts[request_id] and its tree in self.trees[request_id] (and still sets self.count and self.tree). Between counts, new machines can join: the test appends a new child's id to a machine's children. A new count must see them, and an old count keeps its own answer.

Things to get right: the root finishes only when every child has answered, the shape follows the children list and not the arrival order, and a repeated message changes nothing. A machine crashed for good would leave the count waiting forever. It isn't tested here: be ready to say what you'd return instead, and how deep a tree can get before the answer is slow.

solution.py
Run the tests to see which pass.