AlgoMaster Logo
AlgoMasterCalculate Trace Span Exclusive Timehard

Calculate Trace Span Exclusive Time

hard

A trace timeline can contain child spans that run in parallel. Adding or subtracting every child duration independently therefore double-counts overlapping time.

Design a TraceSelfTimeAnalyzer class:

  • TraceSelfTimeAnalyzer() creates a stateless analyzer.
  • int[] exclusiveTimes(int[][] spans) returns every span's exclusive time in input order.
  • int bottleneckSpan(int[][] spans) returns the span ID with the largest exclusive time, breaking ties by smaller ID.

Each row is [spanId, parentId, startTime, endTime] and represents half-open interval [startTime, endTime). The root has parentId = -1. Every child interval lies inside its parent's interval.

A span's exclusive time is its duration minus the time covered by the union of its direct-child intervals. Descendant work is already contained inside a direct child's interval and must not be subtracted again.

Example 1:

Input:

Output:

Explanation: The root's children cover the union [10,80), not 50 + 40 units. Root self time is 100 - 70 = 30; spans 2 and 3 have no children and retain durations 50 and 40.

Example 2:

Input:

Output:

Explanation: Span 20's child intervals merge into [20,70), leaving 80 - 50 = 30. IDs 20, 30, and 40 tie, so ID 20 wins.

Constraints

  • 1 <= spans.length <= 500
  • spans[i].length == 4
  • Span IDs are unique positive integers.
  • Exactly one span has parentId == -1; all other parent IDs exist.
  • 0 <= startTime <= endTime <= 10^9
  • Every child interval is contained in its parent's interval.
  • Parent relationships form a tree, and rows may appear in any order.
  • At most 100 total method calls are made.
Hints

Loading...
CallReturns
new TraceSelfTimeAnalyzer()null
exclusiveTimes([[1,-1,0,100],[2,1,10,60],[3,1,40,80]])[30,50,40]
bottleneckSpan([[1,-1,0,100],[2,1,10,60],[3,1,40,80]])2

The root's child intervals overlap and cover [10,80), or 70 units, so its exclusive time is 30. Span 2 has the largest exclusive time at 50.

Run checks these cases. Submit also runs a larger hidden set.