Count a line network using only neighbor messages and boundary checks. Separate the counting and broadcast passes from constant-time cached queries, and explain readiness, epochs, and topology-change limitations.
Count Nodes in a Line Network through Neighbor Messages
Company: Apple
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Nodes form a left-to-right network. Each node can call isLeftMost() and isRightMost(), sendMessageToLeft(message), and sendMessageToRight(message), and receives messages through receiveMessageFromLeft(message) and receiveMessageFromRight(message). Design a protocol that lets any node answer a request for the total number of nodes.
### Clarifying Questions
Clarify whether the topology is a finite static line, whether messages are reliable and ordered, and whether nodes or links can fail. For the baseline, choose a connected static line, reliable eventual delivery, and a single initialization epoch. No shared-memory traversal API is available. Discuss initialization separately from the latency of later cached queries.
### What a Strong Answer Covers
A concrete message protocol, boundary behavior, initialization readiness, message and time complexity, and the conditions under which a cached total remains valid.
### Follow-up Questions
- How would a new node or failed link invalidate the cached count?
- How would query handling change before initialization finishes?
Overview: Count a line network using only neighbor messages and boundary checks. Separate the counting and broadcast passes from constant-time cached queries, and explain readiness, epochs, and topology-change limitations.
Nodes form a left-to-right network. Each node can call isLeftMost() and isRightMost(), sendMessageToLeft(message), and sendMessageToRight(message), and receives messages through receiveMessageFromLeft(message) and receiveMessageFromRight(message). Design a protocol that lets any node answer a request for the total number of nodes.
Clarifying Questions Guidance
Clarify whether the topology is a finite static line, whether messages are reliable and ordered, and whether nodes or links can fail. For the baseline, choose a connected static line, reliable eventual delivery, and a single initialization epoch. No shared-memory traversal API is available. Discuss initialization separately from the latency of later cached queries.
What a Strong Answer Covers Guidance
A concrete message protocol, boundary behavior, initialization readiness, message and time complexity, and the conditions under which a cached total remains valid.
Follow-up Questions Guidance
How would a new node or failed link invalidate the cached count?
How would query handling change before initialization finishes?