KatInterview

DAG Schedule Analysis and Windowed Checkpoint DP

Dynamic ProgrammingModel answer
HardModel answerDynamic Programmingdagtopological-sortsliding-window

Last updated

Compute critical-path latency and peak concurrency for a task DAG, then find minimum-cost checkpoint placement under a sliding-window coverage constraint.

Background

Execution engines for data pipelines need two things to run efficiently: a way to schedule interdependent tasks as tightly as possible, and a strategy for placing recovery checkpoints without spending more than necessary. You will implement both as methods on a single class.

class PipelineOptimizer:
    def analyze_dag(
        self, n: int, edges: list[list[int]], durations: list[int]
    ) -> list[int]:
        pass

    def min_checkpoint_cost(self, costs: list[int], max_window: int) -> int:
        pass

Part 1: Schedule a Task Graph

You are given n tasks numbered 0 through n - 1. Each task has an integer execution time given by durations[i]. The list edges contains pairs [u, v] meaning task u must finish before task v can start.

Scheduling rules:

  • Every task starts at the earliest non-negative time when all of its prerequisites are done.
  • There is no cap on how many tasks may run at once.
  • A task running during [start, end) occupies that half-open interval. If one task ends at time t and another starts at time t, they are not considered to overlap.

Return [min_latency, max_parallelism]:

  • min_latency — the time at which the last task finishes.
  • max_parallelism — the largest number of tasks executing simultaneously at any single moment.

Example

optimizer = PipelineOptimizer()
optimizer.analyze_dag(
    n=4,
    edges=[[0,1],[0,2],[1,3],[2,3]],
    durations=[3, 2, 4, 1]
)
# Returns: [8, 2]

Task 0 runs [0, 3). Tasks 1 and 2 both become ready at time 3 and run concurrently: task 1 during [3, 5) and task 2 during [3, 7). Their overlap [3, 5) gives peak parallelism 2. Task 3 waits for both and runs [7, 8). Total latency is 8.


Part 2: Cheapest Checkpoint Coverage

A pipeline processes a sequence of m operations. Each operation i has a cost costs[i] to place a checkpoint there.

A checkpoint placement is valid when every contiguous block of max_window consecutive operations contains at least one checkpoint — formally, for every k with 0 <= k <= m - max_window, at least one index in [k, k + max_window) must be chosen.

Return the minimum total cost of a valid placement.

Example

optimizer = PipelineOptimizer()
optimizer.min_checkpoint_cost(
    costs=[3, 2, 4, 1, 5, 2],
    max_window=3
)
# Returns: 3

Placing checkpoints at index 1 (cost 2) and index 3 (cost 1) covers every window of size 3. Total cost is 3.


Constraints

  • 1 <= n <= 2 × 10⁴
  • 0 <= len(edges) <= 5 × 10⁴; the graph is a valid DAG (no cycles).
  • 1 <= durations[i] <= 10⁶
  • 1 <= m == len(costs) <= 10⁵
  • 1 <= max_window <= m
  • 1 <= costs[i] <= 10⁹

What is expected

For each part, explain your approach, argue correctness for the tricky boundary cases (the tie-breaking rule in Part 1; the first and last window positions in Part 2), state time and space complexity, and provide clean working code.

Hints

Hint 1

For Part 1, process nodes in topological order so every predecessor's finish time is locked in before you compute a successor's start time. Then model each task as a half-open interval and think carefully about how to sort end and start events that fall on the same timestamp.

Hint 2

For Part 2, reframe the window constraint as a recurrence: if the most recent checkpoint before position i was at position j, then i - j must be less than max_window or the gap is uncovered. That turns the problem into a 1-D DP where each transition looks back over a fixed-width range of previous states.

Hint 3

The sliding-window minimum over a range of fixed width that advances one step at a time can be maintained in O(1) amortized per step with a monotonic deque. Think about which end of the deque to evict from for out-of-range indices versus dominated values.