Quick Overview

Given a number of courses and a list of prerequisite pairs, decide whether every course can be completed in some order. It tests modeling dependencies between tasks and reasoning about ordering on large inputs, including repeated pairs and courses that list themselves as a prerequisite.

Decide Whether Every Course Can Be Completed Given Prerequisite Pairs

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A program has `num_courses` courses, labeled `0` through `num_courses - 1`. Some courses require others to be completed first. You are given `prerequisites`, where each pair `[a, b]` means course `b` must be completed before course `a` can be taken. Return `True` if there is an order in which every course can be completed, and `False` otherwise. ### Function Signature ```python def can_finish_all(num_courses: int, prerequisites: list[list[int]]) -> bool: ... ``` ### Rules - A course can be taken once all of its prerequisites are complete. There is no limit on how many courses can be taken in total; only the order matters. - A pair may appear more than once; repeats mean the same as a single pair. - A pair `[a, a]` means a course requires itself, which makes completing it impossible. - A course that appears in no pair has no prerequisites. ### Constraints - `1 <= num_courses <= 10^5` - `0 <= len(prerequisites) <= 2 * 10^5` - `0 <= a <= num_courses - 1` and `0 <= b <= num_courses - 1` for every pair. - The output is a single boolean. ### Examples #### Example 1 ```text Input: num_courses = 2, prerequisites = [[1, 0]] Output: True ``` Take course 0, then course 1. #### Example 2 ```text Input: num_courses = 2, prerequisites = [[1, 0], [0, 1]] Output: False ``` Each course requires the other, so neither can be taken first. #### Example 3 ```text Input: num_courses = 4, prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]] Output: True ``` One valid order is 0, 1, 2, 3.

Overview: Given a number of courses and a list of prerequisite pairs, decide whether every course can be completed in some order. It tests modeling dependencies between tasks and reasoning about ordering on large inputs, including repeated pairs and courses that list themselves as a prerequisite.

Read the full Amazon Software Engineer interview experience this question came from

A program has `num_courses` courses, labeled `0` through `num_courses - 1`. Some courses require others to be completed first. You are given `prerequisites`, a list of pairs where each pair `[a, b]` means course `b` must be completed before course `a` can be taken. Return `True` if there is an order in which every course can be completed, and `False` otherwise (`true` or `false` in JavaScript, Java and C++). ### Rules - A course can be taken once all of its prerequisites are complete. There is no limit on how many courses can be taken in total; only the order matters. - A pair may appear more than once; repeats mean the same as a single pair. - A pair `[a, a]` means a course requires itself, which makes completing it impossible. - A course that appears in no pair has no prerequisites. ### Constraints - `1 <= num_courses <= 10^5` - `0 <= len(prerequisites) <= 2 * 10^5` - Every element of `prerequisites` is a pair `[a, b]` with `0 <= a <= num_courses - 1` and `0 <= b <= num_courses - 1`. - Every value fits in a signed 32-bit integer; no value can exceed `2^31 - 1`. - The output is a single boolean. ### Example 1 ```text Input: num_courses = 2, prerequisites = [[1, 0]] Output: True ``` Take course 0, then course 1. ### Example 2 ```text Input: num_courses = 2, prerequisites = [[1, 0], [0, 1]] Output: False ``` Each course requires the other, so neither can be taken first.

Constraints

  • 1 <= num_courses <= 10^5
  • 0 <= len(prerequisites) <= 2 * 10^5
  • Every element of prerequisites is a pair [a, b] with 0 <= a <= num_courses - 1 and 0 <= b <= num_courses - 1
  • A pair may repeat, and a pair may have a equal to b
  • Every value fits in a signed 32-bit integer; no value can exceed 2^31 - 1

Examples

Input: (2, [[1, 0]])

Expected Output: True

Explanation: Source Example 1: take course 0, then course 1.

Input: (2, [[1, 0], [0, 1]])

Expected Output: False

Explanation: Source Example 2: the two courses require each other, a 2-cycle.

Hints

  1. Picture each course as a node and each pair [a, b] as a directed link from course b to course a.
  2. Ask what shape among the links would leave a group of courses each waiting, directly or through others, on another member of the same group. A pair [a, a] is the smallest such group.
  3. With up to 10^5 courses and 2 * 10^5 pairs, aim for time linear in their total, and keep in mind that a chain of prerequisites can be as long as the number of courses.

Check Whether Every Course Can Be Completed

mediumAmazonSoftware Engineer

A program has num_courses courses, labeled 0 through num_courses - 1. Some courses require others to be completed first. You are given prerequisites, a list of pairs where each pair [a, b] means course b must be completed before course a can be taken.

Return True if there is an order in which every course can be completed, and False otherwise (true or false in JavaScript, Java and C++).

Rules

  • A course can be taken once all of its prerequisites are complete. There is no limit on how many courses can be taken in total; only the order matters.
  • A pair may appear more than once; repeats mean the same as a single pair.
  • A pair [a, a] means a course requires itself, which makes completing it impossible.
  • A course that appears in no pair has no prerequisites.

Constraints

  • 1 <= num_courses <= 10^5
  • 0 <= len(prerequisites) <= 2 * 10^5
  • Every element of prerequisites is a pair [a, b] with 0 <= a <= num_courses - 1 and 0 <= b <= num_courses - 1.
  • Every value fits in a signed 32-bit integer; no value can exceed 2^31 - 1.
  • The output is a single boolean.

Example 1

Input: num_courses = 2, prerequisites = [[1, 0]]
Output: True

Take course 0, then course 1.

Example 2

Input: num_courses = 2, prerequisites = [[1, 0], [0, 1]]
Output: False

Each course requires the other, so neither can be taken first.

Examples

Example 1
Input
(2, [[1, 0]])
Output
True
Notes
Source Example 1: take course 0, then course 1.
Example 2
Input
(2, [[1, 0], [0, 1]])
Output
False
Notes
Source Example 2: the two courses require each other, a 2-cycle.

Constraints

  • •1 <= num_courses <= 10^5
  • •0 <= len(prerequisites) <= 2 * 10^5
  • •Every element of prerequisites is a pair [a, b] with 0 <= a <= num_courses - 1 and 0 <= b <= num_courses - 1
  • •A pair may repeat, and a pair may have a equal to b
  • •Every value fits in a signed 32-bit integer; no value can exceed 2^31 - 1

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def can_finish_all(num_courses: int, prerequisites: list[list[int]]) -> bool:
Sample call
can_finish_all(2, [[1, 0]])
Language
Case 1
Input
(2, [[1, 0]])
Expected
True
Case 2
Input
(2, [[1, 0], [0, 1]])
Expected
False

Browse More Questions

Show the approach

Approach

Treat each course as a node and each pair [a, b] as a directed edge from b to a. Every course can be completed exactly when this graph has no directed cycle; a self-pair [a, a] is a cycle of length one. The reference runs Kahn's algorithm. It counts each course's incoming edges, one per pair occurrence, and seeds a queue with every course whose count is zero. Taking a course from the queue means completing it, so each of its outgoing edges lowers the target's count by one, and a target whose count reaches zero joins the queue.

Invariant: every course that enters the queue has all of its prerequisites already completed, so the queue order is a valid completion order. If the graph has no cycle, the courses not yet completed always include one whose remaining count is zero (follow incoming edges backward among them; without a cycle the walk must stop at such a course), so all num_courses courses get completed. If a cycle exists, no course on it ever reaches zero, because each one keeps an incoming edge from the previous course on the cycle, which is never completed either. The answer is therefore whether the number of completed courses equals num_courses.

Edge cases: a repeated pair adds one edge and one to the count per occurrence, and both are consumed together, so repeats change nothing. A course in no pair starts with count zero. A self-pair keeps its course's count above zero forever. The loop is iterative, so a chain as long as 10^5 courses causes no recursion-depth failure.

Time complexity:
O(num_courses + len(prerequisites))
Space complexity:
O(num_courses + len(prerequisites))