FastPrepCritical Path Through Dependent Tasks

Critical Path Through Dependent Tasks

Modular logoModular● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Each task has a unique name, a positive duration, and zero or more prerequisite task names. The dependencies form a directed acyclic graph.

Return a string array whose first value is the maximum total duration of any dependency path and whose remaining values are the task names on that path in execution order. If several paths have the same maximum duration, return the lexicographically smallest task-name sequence.

Function

criticalPathSchedule(taskNames: String[], dependencies: String[][], durations: int[]) → String[]

Examples

Example 1

taskNames = ["A","B","C"]dependencies = [[],[],["A"]]durations = [3,5,5]return = ["8","A","C"]

The dependent path A to C lasts 8, longer than standalone B.

Example 2

taskNames = ["A","B","C","D","E"]dependencies = [[],["A"],["A"],["B","C"],["D"]]durations = [3,2,7,4,2]return = ["16","A","C","D","E"]

The longer branch into D passes through C.

Example 3

taskNames = ["A","B"]dependencies = [[],[]]durations = [4,4]return = ["4","A"]

Equal standalone paths use the lexicographically smaller sequence.

Constraints

  • 1 <= taskNames.length == dependencies.length == durations.length <= 200.
  • Task names are unique nonempty ASCII strings.
  • Every dependency names another task, and the graph is acyclic.
  • 1 <= durations[i] <= 100000; every path total fits a signed 32-bit integer.

More Modular problems

See Modular hiring insights
public String[] criticalPathSchedule(String[] taskNames, String[][] dependencies, int[] durations) {
    // Return {totalDuration, firstTask, ..., lastTask}.
}
taskNames["A","B","C"]
dependencies[[],[],["A"]]
durations[3,5,5]
expected["8", "A", "C"]
Checking account…