Skip to main content
mediumBlind 75

Course Schedule

Given numCourses and prerequisites array where [a, b] means take b before a Return True if you can finish all courses (no cycle in dependency graph) Example: numCourses = 2, prerequisites = [[1,0]] => True Example: numCourses = 2, prerequisites = [[1,0],[0,1]] => False (cycle)

Prerequisites are edges. I peel off every course with nothing blocking it, and whatever refuses to peel is the cycle.

Examples

You write

can_finish(numCourses: int, prerequisites: List[List[int]])

  1. Example 1

    Input
    numCourses = 2, prerequisites = [[1,0]]
    returns
    true
  2. Example 2

    Input
    numCourses = 2, prerequisites = [[1,0],[0,1]]
    returns
    false

The pattern behind it

BFS & DFS

I reach for it when

  • You walk a grid, a graph or a tree and visit each cell once.
  • The word shortest appears and every step costs the same.

Say this before you type. Say why breadth or depth fits, and exactly when a cell counts as visited.

All 52 BFS & DFS problems

Graphs

I reach for it when

  • The input is edges, prerequisites or connections.
  • You need cycles, an ordering or connected groups.

Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.

All 32 Graphs problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Can Finish All Coursesmedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.