Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Backend Engineer Senior programming

You need to implement a function that determines if a sequence of tasks can be completed based on given dependencies between them. Each task is represented by a unique integer ID. You must return a valid order of tasks if possible, or an empty list if not.


Function Signature: def task_order(tasks: List[int], dependencies: List[Tuple[int, int]]) -> List[int]:
Constraints:
  • 1 <= len(tasks) <= 1000

  • len(dependencies) <= 10^4


Example 1:
Input: tasks = [1, 2, 3], dependencies = [(1, 2), (2, 3)]
Output: [1, 2, 3]
Example 2:
Input: tasks = [1, 2, 3], dependencies = [(1, 2), (2, 1)]
Output: [] (There is a cycle)
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free
Link copied to clipboard