Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Coding Round 2 Mid-Level programming

LeetCode #207 - Course Schedule

You are given an integer numCourses representing the total number of courses you have to take, labeled from 0 to numCourses - 1. You are also given an array prerequisites, where prerequisites[i] = [a_i, b_i] indicates that you must take course b_i before course a_i. Return true if you can finish all courses. This requires detecting cycles in a directed graph using DFS or Kahn's algorithm.
Function Signature: def can_finish(numCourses: int, prerequisites: List[List[int]]) -> bool:
Example 1:
Input: numCourses = 2, prerequisites = [[1, 0]]
Output: true
Explanation: There are no cycles so you can take the courses.
Example 2:
Input: numCourses = 2, prerequisites = [[1, 0], [0, 1]]
Output: false
Explanation: There is a cycle in the prerequisites.
Constraints:
  • 1 <= numCourses <= 2000

  • 0 <= prerequisites.length <= 5000

  • prerequisites[i].length == 2.
Structured Response
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free

More Coding Round 2 Interview Prep

LeetCode #159 - Longest Substring with At Most Two Distinct Characters
Coding Round 1 · Mid-Level
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Link copied to clipboard