← Questions

Minimum time to finish target courses in a prerequisite DAG

0
MetaCodingLast asked Dec 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Dec 2025. It tests graphs, topological sort and dynamic programming.

You get a set of courses, how long each one takes, the prerequisite edges between them, and a list of target courses. Return the minimum time needed to finish all the targets. Only the targets and their transitive prerequisites count; other courses, even ones at the same level of the graph, are not required. "Time" can mean two things, so state your scheduling model and answer under both: total study effort (one course at a time), and elapsed time when any number of available courses can run in parallel.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • min_completion_time(duration: list[int], prerequisites: list[list[int]], targets: list[int], parallel: bool) -> int — courses are 0..n-1; a pair [a, b] means b must be finished before a starts. Raise an error if a cycle is reachable from the targets.

Example

duration      = [3, 2, 4, 1, 5, 2]
prerequisites = [[2, 0], [2, 1], [3, 2], [4, 1], [5, 4]]

        0     1
         \   / \
          2     4
          |     |
          3     5

targets = [3]
  required courses: 0, 1, 2, 3        # 4 and 5 are not needed
  parallel = False  ->  10            # 3 + 2 + 4 + 1
  parallel = True   ->  8             # 0 and 1 together (3), then 2 (ends at 7), then 3 (ends at 8)

targets = [3, 5]
  parallel = False  ->  17            # course 1 is shared and counted once
  parallel = True   ->  9             # 1 ends at 2, 4 at 7, 5 at 9; 3 ends at 8

Constraints

  • 1 <= n <= 10^5; 0 <= len(prerequisites) <= 2 * 10^5; 1 <= duration[i] <= 10^4.
  • The graph is expected to be a DAG; targets may be in disconnected parts of it, and one target may be a prerequisite of another.

Clarifying questions

  • Can courses run in parallel, or does time mean total effort?
  • Does a duration mean elapsed time, effort, or both?
  • May one target be a prerequisite of another?
  • What should happen for an unknown target or a cycle?

What a strong answer covers

  • A reverse traversal from every target along prerequisite edges to collect exactly the required courses, with shared prerequisites counted once.
  • Cycle detection limited to the required subgraph, since a cycle elsewhere does not matter.
  • Sequential effort: the sum of the durations of the required courses.
  • Unlimited parallelism: topological order over the required subgraph, finish[c] = duration[c] + max(finish[p] for p in prereqs(c)), and the answer is the largest finish among the targets.
  • Disconnected components handled naturally, without forcing them into one chain.
  • O(V + E) over the required subgraph.

Follow-ups

  • At most two courses can run at the same time. How hard does the problem become?
  • Answer many target-list queries on the same course graph quickly.
  • A course may need any one of several alternative prerequisites. What changes?

Where it's asked

  • Meta · AI-enabled coding · Last asked Dec 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

graphs · topological sort · dynamic programming

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding