PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Return a valid task order

Last updated: Apr 2, 2026

Quick Overview

This question evaluates understanding of graph algorithms and dependency resolution, testing the ability to produce a valid ordering of tasks and recognize when cyclic prerequisites make completion impossible.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Return a valid task order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given `n` tasks labeled from `0` to `n - 1` and a list of prerequisite pairs `prerequisites`, where each pair `[a, b]` means task `b` must be completed before task `a`. Return any valid order in which all tasks can be completed. If it is impossible because the dependency graph contains a cycle, return an empty list. Example: - Input: `n = 4`, `prerequisites = [[1,0],[2,0],[3,1],[3,2]]` - Output: `[0,1,2,3]` or `[0,2,1,3]` Your solution should run efficiently for large inputs.

Quick Answer: This question evaluates understanding of graph algorithms and dependency resolution, testing the ability to produce a valid ordering of tasks and recognize when cyclic prerequisites make completion impossible.

Related Interview Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Implement Event Filtering and Queue Routing - Amazon (medium)
  • Determine if all courses can be completed - Amazon (medium)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
Amazon logo
Amazon
Jan 21, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
1
0
Loading...

You are given n tasks labeled from 0 to n - 1 and a list of prerequisite pairs prerequisites, where each pair [a, b] means task b must be completed before task a.

Return any valid order in which all tasks can be completed. If it is impossible because the dependency graph contains a cycle, return an empty list.

Example:

  • Input: n = 4 , prerequisites = [[1,0],[2,0],[3,1],[3,2]]
  • Output: [0,1,2,3] or [0,2,1,3]

Your solution should run efficiently for large inputs.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.