All practice tasks

Coding (Algo)

Summarize Tree Levels

Tree traversal · Easy

A hierarchy dashboard displays one measurement total for each visible depth. Selecting measurements must not renumber or remove structural levels. A level whose nodes are all unselected still needs a zero so the chart remains aligned with the hierarchy.

Return signed totals in increasing zero-based depth order with one entry for every reachable level.

Input

Call

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

Input shape

{ tree: (integer | null)[], selected?: boolean[] }

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.
  • A null slot removes that node and its entire subtree, even if later descendant slots contain numbers. An omitted selected array selects every node; otherwise it has tree.length booleans. Selection affects contribution only and never removes descendants.

Output

Return value

  • Return an integer array. Empty input or a null root returns [].
  • Keep a zero for every reachable level with no selected contribution. Do not include levels represented only by unreachable stale slots.

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

{
  "selected": [
    false,
    true,
    true
  ],
  "tree": [
    4,
    2,
    3
  ]
}

Returns

[
  0,
  5
]

Example 2

{
  "tree": [
    5,
    null,
    7,
    99
  ]
}

Returns

[
  5,
  7
]

Required complexity

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

What this task practices

Summarize Tree Levels 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.