FastPrepCan All Courses Be Finished
Problems/Can All Courses Be Finished

Can All Courses Be Finished

C3.ai logoC3.ai● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2021 report from the onsite for an entry-level C3.ai data software role lists the classic course schedule problem as one of its three interviews. It gives the name of the problem and nothing more.

This version uses the usual rules: each pair says which course must come first, and you return whether every course can be finished. Courses are labeled from 0, and the limits are our choice.

Our version is about 90% the same as the reported question.

There are numCourses courses, labeled from 0 to numCourses - 1. Each pair prerequisites[i] = [a, b] means you must finish course b before you can take course a.

You take one course at a time. Return true if there is an order in which you can finish every course, and false otherwise.

Function

canFinishAll(numCourses: int, prerequisites: int[][]) → boolean

Examples

Example 1

numCourses = 4prerequisites = [[2,0],[1,2],[3,1]]return = true

Course 2 needs course 0, course 1 needs course 2, and course 3 needs course 1. Take them in the order 0, 2, 1, 3, so the answer is true.

Example 2

numCourses = 3prerequisites = [[0,1],[1,2],[2,0]]return = false

Course 0 needs 1, course 1 needs 2, and course 2 needs 0. Each of the three courses waits for another one in the loop, so none of them can be taken first. The answer is false.

Example 3

numCourses = 5prerequisites = [[3,1],[3,4],[1,0],[4,0],[2,3]]return = true

Course 3 needs courses 1 and 4, both of which need course 0, and course 2 needs course 3. Course 3 can be reached from course 0 along two routes, but that is not a loop. The order 0, 1, 4, 3, 2 works, so the answer is true.

Constraints

  • 1 <= numCourses <= 800
  • 0 <= prerequisites.length <= 3000
  • prerequisites[i].length == 2
  • For each pair [a, b]: 0 <= a, b < numCourses and a != b.
  • No pair appears more than once.

More C3.ai interview questions

  • Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
  • Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
  • Rotate a Square Grid ClockwiseONSITE INTERVIEW · Seen Jan 2023
  • Movie Schedule for Maximum EnjoymentONSITE INTERVIEW · Seen Jan 2023
  • Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
  • Longest Arithmetic SubsequenceONSITE INTERVIEW · Seen Feb 2021
  • Gas Station LoopONSITE INTERVIEW · Seen Dec 2020
  • Drop Recent Duplicate MessagesOA · Seen Oct 2026
  • Maximum K-Star SumOA · Seen Oct 2026
  • Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024

Similar questions at other companies

  • Course SchedulePlaud · ONSITE INTERVIEW
  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Course ScheduleFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
  • Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Trace Terminal Company OwnershipPalantir · ONSITE INTERVIEWPalantir interview questions
  • Course ScheduleNykaa · PHONE SCREENNykaa interview questions
  • Course Schedule FeasibilityTesla · PHONE SCREENTesla interview questions
  • Can You Finish All Courses?Intuit · OAIntuit interview questions
  • Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions

Interview reports that mention this question

  • C3.ai FDE and Other New Grad Roles: 5 Experiences (Part 2 of 2)

C3.ai interview experiences

  • C3.ai New Grad Interviews: 25 Experiences (Part 1 of 2)
  • C3.ai New Grad Forward Deployed Engineer Interview: 1 Experience
All C3.ai interview questionsSee C3.ai hiring insights
Saved
class Solution {
    public boolean canFinishAll(int numCourses, int[][] prerequisites) {
        // Write your code here.
    }
}
Checking account…