Compute a job's total runtime including all of its descendant sub-jobs

Quick Overview

Given parent-child relations between jobs and each job's own runtime, compute the total runtime of one job together with every sub-job beneath it. It tests building an adjacency list from edge pairs and summing over a subtree with an iterative traversal.

Compute a job's total runtime including all of its descendant sub-jobs

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of parent-child job relations and a list of individual job runtimes. Each relation is a pair `[parent, child]` indicating that `child` is a direct sub-job of `parent`. Each runtime entry is a pair `[job_id, runtime]` giving the individual runtime of that job. Given a specific parent job ID, calculate the total runtime for that job. The total runtime is the job's own runtime plus the total runtimes of all its descendant jobs, recursively. The jobs form a tree structure where each job can have zero or more children. Return the total runtime as an integer. ### Function Signature ```python def total_runtime(relations: list[list[str]], runtimes: list[list[str]], parent_job: str) -> int: ``` ### Rules - `[parent, child]` in `relations` means `child` is a direct sub-job of `parent`. The order of the relations does not matter. - Each runtime entry is `[job_id, runtime]`, where `runtime` is a string holding a non-negative decimal integer, such as `"10"`. - Each job has at most one parent, and following child links never leads back to a job. - A job that has no runtime entry contributes 0. - The total covers `parent_job` and every job below it. Jobs above it or in other branches are not counted. ### Constraints - `1 <= number of jobs <= 10^4` - `0 <= runtime of each job <= 1000` - Job IDs are unique strings. - `parent_job` is guaranteed to exist in the job set. - The job relations form a valid tree (no cycles). ### Examples ```text Input: relations = [["A","B"],["A","C"],["B","D"],["B","E"],["C","F"]], runtimes = [["A","10"],["B","5"],["C","8"],["D","3"],["E","2"],["F","7"]], parent_job = "A" Output: 35 ``` A has children B and C, B has children D and E, and C has child F. A is the root, so its total includes every job: 10 + 5 + 8 + 3 + 2 + 7 = 35. ```text Input: relations = [["A","B"],["A","C"],["B","D"],["B","E"],["C","F"]], runtimes = [["A","10"],["B","5"],["C","8"],["D","3"],["E","2"],["F","7"]], parent_job = "B" Output: 10 ``` Only the subtree rooted at B is considered, which holds B, D and E, so the total is 5 + 3 + 2 = 10. ```text Input: relations = [["J3","J4"],["J1","J2"],["J2","J3"]], runtimes = [["J4","6"],["J1","9"],["J3","0"],["J2","4"]], parent_job = "J2" Output: 10 ``` The jobs form the chain J1, J2, J3, J4. The subtree of J2 is J2, J3 and J4, so the total is 4 + 0 + 6 = 10, and J1's 9 is excluded because J1 is above J2.

Overview: Given parent-child relations between jobs and each job's own runtime, compute the total runtime of one job together with every sub-job beneath it. It tests building an adjacency list from edge pairs and summing over a subtree with an iterative traversal.

ServiceNow logo
ServiceNow
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of parent-child job relations and a list of individual job runtimes. Each relation is a pair [parent, child] indicating that child is a direct sub-job of parent. Each runtime entry is a pair [job_id, runtime] giving the individual runtime of that job.

Given a specific parent job ID, calculate the total runtime for that job. The total runtime is the job's own runtime plus the total runtimes of all its descendant jobs, recursively.

The jobs form a tree structure where each job can have zero or more children. Return the total runtime as an integer.

Function Signature

def total_runtime(relations: list[list[str]], runtimes: list[list[str]], parent_job: str) -> int:

Rules

  • [parent, child] in relations means child is a direct sub-job of parent . The order of the relations does not matter.
  • Each runtime entry is [job_id, runtime] , where runtime is a string holding a non-negative decimal integer, such as "10" .
  • Each job has at most one parent, and following child links never leads back to a job.
  • A job that has no runtime entry contributes 0.
  • The total covers parent_job and every job below it. Jobs above it or in other branches are not counted.

Constraints

  • 1 <= number of jobs <= 10^4
  • 0 <= runtime of each job <= 1000
  • Job IDs are unique strings.
  • parent_job is guaranteed to exist in the job set.
  • The job relations form a valid tree (no cycles).

Examples

Input: relations = [["A","B"],["A","C"],["B","D"],["B","E"],["C","F"]], runtimes = [["A","10"],["B","5"],["C","8"],["D","3"],["E","2"],["F","7"]], parent_job = "A"
Output: 35

A has children B and C, B has children D and E, and C has child F. A is the root, so its total includes every job: 10 + 5 + 8 + 3 + 2 + 7 = 35.

Input: relations = [["A","B"],["A","C"],["B","D"],["B","E"],["C","F"]], runtimes = [["A","10"],["B","5"],["C","8"],["D","3"],["E","2"],["F","7"]], parent_job = "B"
Output: 10

Only the subtree rooted at B is considered, which holds B, D and E, so the total is 5 + 3 + 2 = 10.

Input: relations = [["J3","J4"],["J1","J2"],["J2","J3"]], runtimes = [["J4","6"],["J1","9"],["J3","0"],["J2","4"]], parent_job = "J2"
Output: 10

The jobs form the chain J1, J2, J3, J4. The subtree of J2 is J2, J3 and J4, so the total is 4 + 0 + 6 = 10, and J1's 9 is excluded because J1 is above J2.