KatInterview

Tallying a Tree of Message-Passing Processes

Trees & GraphsModel answer
MediumModel answerTrees & Graphsdistributed-systemsconvergecastidempotency

Last updated

Each tree node is an isolated process with only async send and blocking receive. Design a protocol so the root learns the tree size, tolerates lost and duplicate messages, and returns a structured result.

A rooted tree is deployed so that every node is a separate process. Processes have no shared memory. They interact only through these two primitives:

send_async(to_id, message)   # returns at once; delivery happens later
receive()                    # blocks until something arrives; returns (sender_id, message)

Each process knows its own id, its parent's id (the root has none) and its children's ids. It knows nothing else about the tree.

Design the program that every node runs so that the root finds out how many nodes the tree contains. Then make it robust to an unreliable network, and finally decide what shape the result should take if a caller wants more than a single integer.

Before you start, state your assumptions: how the computation is triggered, whether per-pair message ordering is guaranteed, whether receive can time out, and whether processes can crash or only messages can be lost.

Part 1 - Reliable delivery. Every message arrives exactly once, after an arbitrary delay and possibly out of order. Write the per-node logic and explain why the root's total is right. Give the message count and the completion time in terms of the number of nodes n and the tree depth d.

Part 2 - Unreliable delivery. Any message may be lost, and any message may be delivered more than once. Adapt the protocol so the root still gets the correct count when it can. Cover how duplicates are neutralised, who retries and when, how a node reacts to a repeated request it has already answered, and what happens if a subtree never responds.

Part 3 - Richer result. The caller needs more than one integer. Define what each reply carries and what the root returns. Discuss how message size grows with subtree size, and how the result expresses that the count may be incomplete.

Hints

Hint 1

Think of it as a two-phase wave: a request travels down, and answers travel back up. A node should answer its parent only when every child has answered, and it should track children by id rather than relying on arrival order.

Hint 2

For unreliable networks, make every handler idempotent. Store each child's reply in a map keyed by child id so a second copy overwrites or is ignored, and have the parent (the side that is waiting) retry requests on a timer.

Hint 3

For the richer result, return something like a status plus a count plus the list of unreachable child ids, and compare a full per-node listing (message size O(subtree)) with a compact summary (O(1) per message).