Rebuild distributed trace span trees with orphan roots and return pre-order depths

Quick Overview

Rebuild a forest of distributed tracing spans from an unordered list of span and parent ids, treating spans with a missing parent as orphan roots, and return each span with its depth in pre-order. It tests building a tree from parent links with a hash map, deterministic child ordering and iterative depth-first traversal.

Rebuild distributed trace span trees with orphan roots and return pre-order depths

Company: Datadog

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

In distributed tracing, a single request produces many spans that can arrive in any order. You are given an unordered list `spans`, where `spans[i] = [span_id, parent_id]`. Every `span_id` is unique. A `parent_id` of `-1` means the span has no parent and is a root. If a span's `parent_id` points to a span that is not in the list (because the parent has not arrived yet), treat that span as an orphan root. Rebuild the forest of trace trees. Return it as a list of `[span_id, depth]` pairs in pre-order traversal. Roots have depth `0`. Visit the roots in ascending order of `span_id`, and visit the children of every span in ascending order of `span_id`. ### Function Signature ```python def build_span_trees(spans: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each input entry is `[span_id, parent_id]`. A span is a root when `parent_id == -1` or when no entry in `spans` has `span_id == parent_id`. The value `0` is a valid span id, not a missing parent. - Pre-order means a span is listed before all of its descendants, and a span's whole subtree is listed before its next sibling. - Roots are visited in ascending `span_id` order, and the children of each span are visited in ascending `span_id` order. This makes the output unique. - The depth of a span is the number of edges from it up to its root. An orphan root has depth `0`, and its descendants count from it. - The output contains every span exactly once. ### Constraints - `1 <= len(spans) <= 10^4` - `len(spans[i]) == 2` - `0 <= span_id <= 10^9` - `parent_id == -1` or `0 <= parent_id <= 10^9` - All `span_id` values are unique - `parent_id != span_id` - The parent links among the spans in the list contain no cycles ### Examples ```text Input: spans = [[3,1],[1,-1],[2,1],[4,2],[5,-1],[6,5]] Output: [[1,0],[2,1],[4,2],[3,1],[5,0],[6,1]] ``` There are two roots, 1 and 5. Spans 2 and 3 are children of 1, span 4 is a child of 2, and span 6 is a child of 5, so in pre-order with children taken in ascending order the visit order is 1(0), 2(1), 4(2), 3(1), 5(0), 6(1). ```text Input: spans = [[10,7],[20,10],[30,-1]] Output: [[10,0],[20,1],[30,0]] ``` Span 7 is not in the list, so span 10 is an orphan root with span 20 below it. The roots are 10 and 30, visited in that order.

Overview: Rebuild a forest of distributed tracing spans from an unordered list of span and parent ids, treating spans with a missing parent as orphan roots, and return each span with its depth in pre-order. It tests building a tree from parent links with a hash map, deterministic child ordering and iterative depth-first traversal.

Datadog logo
Datadog
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

In distributed tracing, a single request produces many spans that can arrive in any order. You are given an unordered list spans, where spans[i] = [span_id, parent_id]. Every span_id is unique.

A parent_id of -1 means the span has no parent and is a root. If a span's parent_id points to a span that is not in the list (because the parent has not arrived yet), treat that span as an orphan root.

Rebuild the forest of trace trees. Return it as a list of [span_id, depth] pairs in pre-order traversal. Roots have depth 0. Visit the roots in ascending order of span_id, and visit the children of every span in ascending order of span_id.

Function Signature

def build_span_trees(spans: list[list[int]]) -> list[list[int]]:

Rules

  • Each input entry is [span_id, parent_id] . A span is a root when parent_id == -1 or when no entry in spans has span_id == parent_id . The value 0 is a valid span id, not a missing parent.
  • Pre-order means a span is listed before all of its descendants, and a span's whole subtree is listed before its next sibling.
  • Roots are visited in ascending span_id order, and the children of each span are visited in ascending span_id order. This makes the output unique.
  • The depth of a span is the number of edges from it up to its root. An orphan root has depth 0 , and its descendants count from it.
  • The output contains every span exactly once.

Constraints

  • 1 <= len(spans) <= 10^4
  • len(spans[i]) == 2
  • 0 <= span_id <= 10^9
  • parent_id == -1 or 0 <= parent_id <= 10^9
  • All span_id values are unique
  • parent_id != span_id
  • The parent links among the spans in the list contain no cycles

Examples

Input: spans = [[3,1],[1,-1],[2,1],[4,2],[5,-1],[6,5]]
Output: [[1,0],[2,1],[4,2],[3,1],[5,0],[6,1]]

There are two roots, 1 and 5. Spans 2 and 3 are children of 1, span 4 is a child of 2, and span 6 is a child of 5, so in pre-order with children taken in ascending order the visit order is 1(0), 2(1), 4(2), 3(1), 5(0), 6(1).

Input: spans = [[10,7],[20,10],[30,-1]]
Output: [[10,0],[20,1],[30,0]]

Span 7 is not in the list, so span 10 is an orphan root with span 20 below it. The roots are 10 and 30, visited in that order.