TRT
Sign in
easyCorecoding

Find the diameter of a binary tree in one pass

Explain why the longest path turns at some node, and compute it with one postorder pass that returns heights.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstTree recursion patternsPro — members onlyMost tree problems are solved by one of three recursive shapes: combine the children's answers on the way up, pass information down from the root, or return one value while updating a global best.

A strong answer

A strong candidate frames Find the diameter of a binary tree in one pass around the exact memory layout, compiler semantics, and hardware execution model covered in Tree recursion patterns. Explain why the longest path turns at some node, and compute it with one postorder pass that returns heights.

Every subtree of a tree is itself a tree. That makes recursion the natural tool: a function that solves a problem for a tree can call itself on the two subtrees and combine the results. The empty tree is the base case, and it is worth choosing its answer carefully, because a good choice removes special cases for leaves and missing children.

Height, size, the sum of all values, whether two trees are identical, and the minimum and maximum of a subtree all follow this shape. The work happens after the recursive calls return, so it is a postorder traversal. Each node is visited once, for O(n) time.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Solve problems bottom-up by combining the answers of the two subtrees.
  • Mastery of core invariant: Solve problems top-down by passing information from the root toward the leaves.
  • Mastery of core invariant: Return one value while tracking another, as in the diameter and maximum path sum.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How would you also return the path itself, not just its length?
  2. 02How does the solution change if nodes have weights, possibly negative?
  3. 03What is the diameter of a general tree where nodes can have many children?
  4. 04Why is computing height separately at every node slower?

Not started

Sign in to save your learning progress.

Sign in to save