All practice tasks

Coding (Algo)

Depth of a Nested Index

Depth first search · Easy

A hierarchy inspector reads a serialized binary tree and reports its maximum nesting depth. Missing nodes can leave unreachable values later in the array. Array length alone therefore does not reveal the true depth.

Return the number of reachable nodes on the longest path from the root to a leaf.

Input

Call

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

Input shape

A JSON object { "tree": (number | null)[] }.

Fields

  • tree is a heap-indexed array: children of index i are 2i+1 and 2i+2. Null or an out-of-range index has no node. Entries below a missing ancestor are unreachable and ignored.

Output

Return value

  • Return an integer node count, not an edge count.
  • An absent root has depth 0. A root with no children has depth 1.
  • Ignore all entries whose ancestor is missing.

Constraints

  • 0 <= tree.length <= 100000. Node values are integers in [-1000000000,1000000000].
  • This is not a compact breadth-first serialization: missing child slots keep their indexes.

Examples

Example 1

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

Returns

3

The path 1→2→4 has three nodes.

Example 2

{
  "tree": [
    null,
    1,
    2
  ]
}

Returns

0

There is no root.

Required complexity

O(n) time and O(h) traversal space

What this task practices

Depth of a Nested Index 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 easy 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.