Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deterministic Task Execution Order

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Deterministic Task Execution Order. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A fintech payments platform runs thousands of nightly jobs: fraud model refreshes, chargeback exports, ledger reconciliation, and regulatory reports. A single job running too early can invalidate downstream results and trigger compliance alerts. Your orchestration service receives a dependency graph of tasks and must produce a deterministic execution plan.

You are given n tasks labeled 0..n-1 and a list of dependencies deps, where each pair [a, b] means task a depends on task b (so b must execute before a).

Return a list representing a valid execution order that satisfies all dependencies. If multiple valid orders exist, return the one that is lexicographically smallest (i.e., at each choice point, pick the smallest task id available). If it is impossible to schedule all tasks due to a cycle, return an empty list.

Function Signature

  • Input: n: int, deps: list[list[int]]
  • Output: list[int]

Notes

  • You must include all tasks 0..n-1 in the output if a schedule exists (even tasks that are isolated).
  • Lexicographically smallest requirement implies you should not return an arbitrary topological order.

Constraints

  • 1 <= n <= 2 * 10^5
  • 0 <= deps.length <= 3 * 10^5
  • 0 <= a, b < n
  • a != b
  • deps may contain duplicate pairs; treat duplicates as a single dependency
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output