All practice tasks

Coding (Algo)

Measure the Longest Tree Route

Tree traversal · Medium

A hierarchy inspection tool measures the longest route between any two reachable nodes. A route can move up to a shared ancestor and down another branch. The longest route may lie wholly inside a subtree rather than passing through the root.

Return the greatest number of edges on any simple route between two reachable nodes in the tree.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ tree: (integer | null)[] }

Fields

  • tree is a binary tree encoded with heap indexes: root 0; children of i are 2i+1 and 2i+2. This is not compact queue serialization.
  • Null parents make their descendants unreachable. All edges have length one and values are irrelevant.

Output

Return value

  • Return an integer edge count, not a number of nodes. Empty and singleton trees both return zero.
  • The route can begin and end anywhere in the reachable tree; it need not pass through the root.

Constraints

  • 0 <= tree.length <= 100000; each non-null value is an integer from -1000000000 to 1000000000.
  • Do not mutate either input array. Sums fit safe JSON integers. Heap indexing bounds the tree height by 17 at this input limit.

Examples

Example 1

{
  "tree": [
    1,
    2,
    3,
    4,
    5
  ]
}

Returns

3

Example 2

{
  "tree": [
    1
  ]
}

Returns

0

Required complexity

O(n) time and O(h) auxiliary traversal space excluding the returned output

What this task practices

Measure the Longest Tree Route is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.