Count Nodes in a Line Network through Neighbor Messages

Read the full interview experience this question came from →

Quick 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.

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.

Read the full Apple Software Engineer interview experience this question came from

Apple logo
Apple
Aug 26, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

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?