← All Exercises
Reconstruct a Trace from Span Records
intermediatejavascript
Background
Tracing backends store flat span rows. Reconstructing the tree client-side makes it possible to reason about which entry point absorbed the latency.
Problem
Build the span tree from parent references, list root spans with child counts and durations, identify the slowest root, and report spans whose parent is missing.
Input
JSON object: { spans: [{ id, parentId?, name, durationMs }] }Output
JSON object: { roots, slowestRoot, orphanIds }Sample tests
Sample 1
Input: {"spans":[{"id":"r1","name":"GET /checkout","durationMs":120},{"id":"c1","parentId":"r1","name":"db query","durationMs":80},{"id":"r2","name":"GET /health","durationMs":3}]}Expected: {"roots":[{"id":"r1","name":"GET /checkout","childCount":1,"durationMs":120},{"id":"r2","name":"GET /health","childCount":0,"durationMs":3}],"slowestRoot":"GET /checkout","orphanIds":[]}Sample 2
Input: {"spans":[{"id":"r1","name":"worker job","durationMs":50},{"id":"orphan","parentId":"missing","name":"publish","durationMs":10}]}Expected: {"roots":[{"id":"r1","name":"worker job","childCount":0,"durationMs":50}],"slowestRoot":"worker job","orphanIds":["orphan"]}Your solution
Learning objective
Reconstruct trace structure and attribute latency to the right entry point.