Apple Coding Question: Count Occurrences in Binary Tree

Question Description

Problem overview

You are asked to count how many nodes in a binary tree contain a given target value. The baseline task is a correct, single-threaded implementation that traverses the tree and returns the exact count. Subsequent stages extend that core: one requires a parallel implementation that divides traversal among workers and safely aggregates partial counts, and another asks you to build an index or cache to answer repeated queries quickly.

What you'll be asked to do (flow / stages)

  1. Implement count_occurrences(root, target) using a deterministic traversal (DFS or BFS) that returns the exact integer count.
  2. Extend your solution to count_occurrences_parallel(root, target, num_workers) — partition work across multiple workers and combine results with thread-safe aggregation (e.g., per-worker counters + atomic sum or a mutex-protected accumulator). Document thread-safety properties and shared state usage.
  3. Design build_index(root) and query_index(index, target) to precompute an auxiliary map from value -> frequency so repeated queries run in O(1) time. Optionally describe update operations and their consistency guarantees.

Skills and signals interviewers look for

  • Correct traversal of binary trees (recursive or iterative DFS/BFS).
  • Careful handling of concurrency: partitioning strategy, synchronization (locks, atomics, or concurrent accumulators), and avoiding lost updates.
  • Time/space trade-offs for precomputation (hash map of frequencies), and how updates affect index consistency.
  • Clear function signatures and complexity analysis (O(n) build, O(1) query, O(n/num_workers) per worker ideally).

You should be prepared to discuss partitioning strategies (subtree work queues, task splitting), synchronization choices, memory vs latency trade-offs, and how your index handles dynamic changes.

Common Follow-up Questions

  • How would you partition the tree for N workers to balance load and minimize synchronization overhead?
  • Describe a lock-free or wait-free aggregation approach for combining worker partial counts; what primitives would you use?
  • If tree updates (insert/delete) are frequent, how would you maintain an index so queries remain consistent and efficient?
  • How does tail recursion or iterative traversal affect stack usage and concurrency when splitting tasks?
  • What changes if node values are not equality-comparable but need a custom comparator or hashing for the index?

Related Questions

1Binary Tree Path Sum — Amazon Coding Question Guide
2Meta Coding Question: Indent Root-to-Leaf Paths by Column
3Anthropic Coding Interview: Domain-Scoped Web Crawler
4Atlassian Coding Question: URL Router with Wildcards
5Bytedance Data Structures Interview: CS Foundation Topics
6Count distinct values in a binary tree and return frequency map
7Find kth occurrence of a value in tree or return -1 if fewer than k
8Parallel tree traversal patterns: work-stealing, BFS-level partitioning, and worker queues
9Design an index to support value range queries in a binary tree (e.g., count values in [a, b])
10How to make tree traversal fault-tolerant in a multi-threaded environment

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Count Occurrences in Binary Tree - Apple Coding Interview | Voker