PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of graph modeling, directed cycle detection, and dependency resolution in the context of course prerequisites. It is commonly asked in the coding & algorithms domain because it reveals a candidate's algorithmic problem-solving ability and practical application of graph algorithms (expected O(V+E) time), testing practical implementation skills rather than only conceptual theory.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Machine Learning Engineer

Determine Whether Courses Can Be Completed

Company: Snapchat

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an integer `numCourses`, representing courses labeled from `0` to `numCourses - 1`, and a list `prerequisites`. Each prerequisite is a pair `[course, prerequisite]`, meaning you must finish `prerequisite` before taking `course`. Return `true` if it is possible to finish all courses. Return `false` if the prerequisite relationships contain a cycle that makes completion impossible. Example: ```text Input: numCourses = 2, prerequisites = [[1, 0]] Output: true Explanation: Take course 0 first, then course 1. ``` Example: ```text Input: numCourses = 2, prerequisites = [[1, 0], [0, 1]] Output: false Explanation: Course 0 requires course 1, and course 1 requires course 0, so there is a cycle. ``` Expected complexity: `O(V + E)` time, where `V` is the number of courses and `E` is the number of prerequisite pairs.

Quick Answer: This question evaluates understanding of graph modeling, directed cycle detection, and dependency resolution in the context of course prerequisites. It is commonly asked in the coding & algorithms domain because it reveals a candidate's algorithmic problem-solving ability and practical application of graph algorithms (expected O(V+E) time), testing practical implementation skills rather than only conceptual theory.

You are given an integer numCourses representing courses labeled from 0 to numCourses - 1, and a list prerequisites. Each prerequisite pair [course, prerequisite] means you must complete prerequisite before taking course. Return True if it is possible to finish all courses, and False if the prerequisite relationships contain a cycle that makes completing every course impossible.

Constraints

  • 1 <= numCourses <= 100000
  • 0 <= len(prerequisites) <= 200000
  • Each prerequisite is a pair [course, prerequisite] with 0 <= course, prerequisite < numCourses

Examples

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

Expected Output: True

Explanation: Course 0 has no prerequisite, so you can take 0 first and then 1.

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

Expected Output: False

Explanation: Course 0 depends on 1 and course 1 depends on 0, forming a cycle.

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

Expected Output: True

Explanation: One valid order is 0, 1, 2, 3. There is no cycle.

Input: (3, [])

Expected Output: True

Explanation: With no prerequisites, all courses can be taken in any order.

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

Expected Output: False

Explanation: The only course depends on itself, which is a cycle.

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

Expected Output: True

Explanation: Even with a duplicate prerequisite pair, there is still no cycle. A valid order is 0, 1, 2.

Hints

  1. Model the courses as a directed graph where prerequisite -> course is a directed edge.
  2. If you repeatedly take courses with no remaining prerequisites and still cannot process all courses, then a cycle exists.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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.

Related Coding Questions

  • Solve Decimal Coin Change - Snapchat (medium)
  • Find Maximum Island Perimeter - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a Timestamped Counter - Snapchat (medium)
  • Implement a custom list with iterator and map - Snapchat (medium)