FastPrepCount Nodes in a Chain
Problems/Count Nodes in a Chain

Count Nodes in a Chain

Apple logoApple● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Apple full-time phone screen reports describe this. One says each node can only reach its left and right nodes, and you count how many nodes there are in total. The other calls it Network Node Count. You get a Node class and calls for left and right messages and state checks. You design a function so that any node, when asked for the total, works it out by talking to other nodes.

The reports do not give an input format. Here, left and right arrays and a start index stand in for the nodes. An empty pool has start = -1 and returns 0, the pool can hold other chains that do not count, and the links are always valid. The message calls are described in the statement and discussed in the editorial, but they are not checked. Every node would return the same count, so the output cannot show that messages were used.

Our version is about 60% the same as the reported questions.

You are given a starting node in a finite doubly linked chain. Each node exposes only its left and right neighbor. Count the nodes in that chain.

For this exercise, arrays left and right describe a set of nodes that you cannot change: node i has neighbors left[i] and right[i]. A neighbor of -1 means that side ends. Start at start and follow neighbor links; do not infer chain length from node identifiers or the size of the storage pool.

The pool may contain other disconnected chains. Return the size of the chain containing start, without counting nodes in any other chain. If the pool is empty, start = -1 and the answer is 0. The links agree in both directions and contain no cycles. Do not modify them.

Another version of this question: counting by messages

The same question can also be framed as a network of nodes that can only talk to their neighbors. Each node has these calls: isLeftMost() and isRightMost() to check whether it is at an end, sendMessageToLeft(Message m) and sendMessageToRight(Message m) to send a message to a neighbor, and receiveMessageFromLeft(Message m) and receiveMessageFromRight(Message m) to receive one. When any node gets a request for the total node count, it must work out the answer through messages between nodes and return it.

The function above does not simulate messages. Every node would return the same number, the node count, so checking the output cannot show that messages were used. The editorial covers this version.

Function

countChainNodes(left: int[], right: int[], start: int) → int

Examples

Example 1

left = [2,3,-1,0]right = [3,-1,0,1]start = 3return = 4

Start at node 3. Going left reaches nodes 0 and 2, and going right reaches node 1. With node 3 itself, that is 4 nodes.

Example 2

left = []right = []start = -1return = 0

The pool has no nodes and start is -1, so the chain has 0 nodes.

Example 3

left = [-1,-1,0,1,3]right = [2,3,-1,4,-1]start = 3return = 3

Start at node 3. Going left reaches node 1, and going right reaches node 4. That is 3 nodes. The chain 0, 2 is separate and is not counted.

Constraints

  • 0 <= left.length = right.length <= 1024.
  • Nonempty input has 0 <= start < left.length.
  • Every neighbor is -1 or a valid node index.
  • If right[i] = j, then left[j] = i, and conversely.
  • Every component is a simple finite chain, including a possible isolated node.

More Apple Phone Screen questions

  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
  • Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
  • Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
  • Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
  • Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
  • Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
  • Detect a Cycle in a Linked ListSalesforce · PHONE SCREENSalesforce interview questions
  • Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
  • Reverse a Linked ListMygate · PHONE SCREENMygate interview questions
  • Linked List Palindrome in Constant SpaceNVIDIA · PHONE SCREENNVIDIA interview questions
  • First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
  • Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Merge Two Sorted Linked ListsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int countChainNodes(int[] left, int[] right, int start) {
        // write your code here
    }
}
Checking account…