Course Prerequisite Feasibility
Updated
Problem statement
There are n courses numbered from 0 to n - 1. Each row [course, previous] in prerequisites means that previous must be completed before course.
Complete canFinish. Return true if every course can be completed in some order, or false if the prerequisite rules make that impossible. Each course is completed once; there are no durations, semester limits, or resource constraints.
Repeated rows state the same rule and do not change the answer. A course that requires itself cannot be completed. You must complete every course, including courses that have no prerequisite connection to the others.
Function
canFinish(n: int, prerequisites: int[][]) → booleanExamples
Example 1
n = 2prerequisites = [[1,0]]return = trueTake course 0 before course 1.
Example 2
n = 2prerequisites = [[1,0],[0,1]]return = falseEach course requires the other, so neither can be completed first.
Example 3
n = 3prerequisites = []return = trueWith no prerequisites, every course can be completed.
Constraints
1 <= n <= 200.0 <= prerequisites.length <= 5000.- Every prerequisite row has exactly two integers in
[0, n - 1]. Duplicate rows and self-prerequisites are permitted.
More Snowflake Phone Screen questions
- Minimum StackPHONE SCREEN · Seen Nov 2026
- Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Course ScheduleByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Dependency-Aware Task SchedulerRippling · PHONE SCREENRippling interview questions
- Build Packages as Dependencies Are DiscoveredMongoDB · PHONE SCREENMongoDB interview questions