FastPrepTotal Duration of Related Jobs
Problems/Total Duration of Related Jobs

Total Duration of Related Jobs

Datadog logoDatadog● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand report asks for the time needed for a job and its related child jobs, given job durations and child pairs. The report does not specify parallel execution or shared descendants. This practice version uses a single worker, counts each reachable job once, and defines indexed IDs, valid graphs and bounds. The core task matches the report at about 75%.

Jobs have IDs 0 through durations.length - 1. Job i takes durations[i] time units. A pair [parent, child] means that child is a child job of parent.

Starting with startJob, find that job and every job reachable by following zero or more child edges. Return the sum of their durations. Count a shared descendant only once, even when several paths reach it. Ignore unrelated jobs.

Scheduling rule for this exercise: the selected jobs run once each on a single worker. The returned sum is their total work and elapsed execution time under this rule. It is not an unlimited-parallel-worker completion time. A valid dependency order exists because the graph is acyclic; you do not need to return that order.

Function

relatedJobDuration(durations: long[], childPairs: int[][], startJob: int) → long

Examples

Example 1

durations = [3,5,7,11]childPairs = [[0,1],[0,2],[1,3],[2,3]]startJob = 0return = 26

The selected jobs are 0,1,2,3. Job 3 is reachable through both 1 and 2, but runs once. The sum is 3 + 5 + 7 + 11 = 26.

Example 2

durations = [8,2,4,100]childPairs = [[0,1],[1,2]]startJob = 1return = 6

Starting at job 1 selects 1 and its child 2. Job 0 is an ancestor, and job 3 is unrelated; neither is selected. Return 2 + 4 = 6.

Example 3

durations = [0]childPairs = []startJob = 0return = 0

The starting job is still selected. Its duration is zero, so the total is zero.

Constraints

  • 1 <= durations.length <= 200
  • 0 <= durations[i] <= 1000000000
  • 0 <= childPairs.length <= 1000
  • Every row of childPairs has exactly two integers in [0, durations.length - 1].
  • 0 <= startJob < durations.length
  • The directed child graph is acyclic, has no self edges, and has no repeated edges.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public long relatedJobDuration(long[] durations, int[][] childPairs, int startJob) {
    // Write your code here.
}
Checking account…