Determine Whether Courses Can Be Completed
Company: Snapchat
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
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.
Hints
- Model the courses as a directed graph where prerequisite -> course is a directed edge.
- If you repeatedly take courses with no remaining prerequisites and still cannot process all courses, then a cycle exists.