Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Course order

    Course order

    Mediumgraphtopological sortheap

    There are n courses 0 to n-1. Each [a, b] in prereqs means b must come before a. Return an order that takes every course. If several orders work, return the lexicographically smallest (always take the smallest available course number). If impossible (a cycle), return [].

    Example 1

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

    Example 2

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

    Example 3

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

    Constraints

    • 1 ≤ n ≤ 2000
    • 0 ≤ prereqs.length ≤ 5000
    • No repeated pairs

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

    /**
     * @param {int} n
     * @param {int[][]} prereqs
     * @return {int[]}
     */
    function findOrder(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.