Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Can you finish every course?

    Can you finish every course?

    Mediumgraphtopological sortbfs

    There are n courses numbered 0 to n - 1. Each [a, b] in prereqs means you must finish b before a (like JavaScript before React). Return true if it's possible to finish every course.

    Example 1

    Input:  n = 2, prereqs = [[1,0]]
    Output: true

    Example 2

    Input:  n = 2, prereqs = [[1,0],[0,1]]
    Output: false

    Each course waits for the other: a cycle.

    Constraints

    • 1 ≤ n ≤ 2000
    • 0 ≤ prereqs.length ≤ 5000

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {int} n
     * @param {int[][]} prereqs
     * @return {bool}
     */
    function canFinish(n, prereqs) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.