KatInterview

Costliest Root Task in a Task Forest

Trees & GraphsAuto-graded · 17 test cases
MediumAuto-gradedTrees & Graphstreedfshash-map

Last updated

Sum durations over each root's subtree in a parent-linked record list and return the root with the largest total, testing tree aggregation and iterative traversal.

Costliest Root Task in a Task Forest

A scheduler logs its tasks as a flat list of records. Each record is a dictionary with these keys:

  • id: a unique identifier for the task.
  • parent_id: the id of the task that launched this one. For a root task, this key is either None or absent altogether.
  • duration_ms: a number giving how long this task ran, in milliseconds.

Tasks can launch other tasks, which can launch still more, so the records form a forest of trees. The total cost of a root task is its own duration_ms plus the duration_ms of every task below it, at any depth.

Write a function that finds the root task with the greatest total cost and returns it together with that cost.

Output

  • Return a dictionary {"id": <root id>, "total_cost_ms": <total cost>}.
  • If several roots share the maximum total cost, return the one that appears earliest in the input list.
  • If the list is empty, or no record is a root, return None.

Rules

  • A record is a root when parent_id is missing or equal to None. Other falsy values, such as 0 or "", are real parent ids and do not make a task a root.
  • Only descendants of a root count toward that root's total. A task whose parent_id points to an id that does not exist, or one that sits in a parent cycle, is not reachable from any root and is ignored.
  • Chains can be very deep, so avoid relying on Python's default recursion limit.

Constraints

  • 0 <= len(job_records) <= 10^5
  • All id values are unique and hashable (strings or integers).
  • Every record has a numeric duration_ms; totals are compared as plain numbers.
  • Records may appear in any order; a child may be listed before its parent.
  • The depth of a tree can reach 10^5.

Examples

Example 1

Input: costliest_root_job([{'id': 'A', 'parent_id': None, 'duration_ms': 5}, {'id': 'B', 'parent_id': 'A', 'duration_ms': 1}, {'id': 'C', 'parent_id': 'A', 'duration_ms': 3}, {'id': 'D', 'parent_id': None, 'duration_ms': 20}])

Output: {'id': 'D', 'total_cost_ms': 20}

Example 2

Input: costliest_root_job([{'id': 'root', 'duration_ms': 10}, {'id': 'child', 'parent_id': 'root', 'duration_ms': 2}, {'id': 'grandchild', 'parent_id': 'child', 'duration_ms': 4}])

Output: {'id': 'root', 'total_cost_ms': 16}

Example 3

Input: costliest_root_job([{'id': 'A', 'parent_id': None, 'duration_ms': 5}, {'id': 'B', 'parent_id': None, 'duration_ms': 5}])

Output: {'id': 'A', 'total_cost_ms': 5}

Hints

Hint 1

Each record only points upward to its parent, but summing a tree means walking downward. Build a map from each parent id to the list of its child ids.

Hint 2

Only the totals of the roots matter. For each root, a simple traversal that adds up every reachable duration is enough, with no per-node subtree sums needed.

Hint 3

Use an explicit stack instead of recursion to survive very deep chains. Start with the first root as the best, then replace it only on a strictly greater total, so ties keep the earliest root.