FastPrepLexicographically Smallest Course Order
Problems/Lexicographically Smallest Course Order

Lexicographically Smallest Course Order

Snowflake logoSnowflake● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The named course-ordering exercise is about 90% the same as the reported question. This exercise comes from a collection of interview-prep questions, not from one dated interview. The smallest-ID tie rule, the function shape, the input limits and the examples are our choices, not extra facts from the report.

There are n courses numbered from 0 to n - 1. Each row [course, previous] in prerequisites means you must take previous before course.

Complete findOrder. Return an array containing every course exactly once in an order that respects all prerequisites. If no such complete order exists, return an empty array.

For this practice exercise, choose the lexicographically smallest valid order: at the first position where two valid orders differ, the order with the smaller course ID is smaller. This is equivalent to always choosing the smallest currently available course.

Repeated prerequisite rows do not change the rules. A course may list itself as a prerequisite; in that case, no complete order is possible. Include every course, even those with no prerequisites or no connection to other courses. There are no course durations or limits on semesters or resources.

Function

findOrder(n: int, prerequisites: int[][]) → int[]

Examples

Example 1

n = 4prerequisites = [[1,0],[2,0],[3,1],[3,2]]return = [0,1,2,3]

Start with 0; then 1 and 2 are available, so choose 1 first. Course 3 follows both.

Example 2

n = 2prerequisites = [[1,0],[0,1]]return = []

Each course requires the other, so no complete order exists.

Example 3

n = 4prerequisites = [[0,2],[1,2]]return = [2,0,1,3]

Courses 2 and 3 are initially available. After 2, newly unlocked 0 and 1 come before 3 under the smallest-ID tie rule.

Constraints

  • 1 <= n <= 200.
  • 0 <= prerequisites.length <= 5000.
  • Every prerequisite row has exactly two integers in [0, n - 1]. Duplicate rows and self-prerequisites are permitted.

More Snowflake Phone Screen questions

  • Minimum StackPHONE SCREEN · Seen Nov 2026
  • Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
  • Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
  • Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
  • Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
  • Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
  • Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
  • Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
  • Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Task Build Order and File-Change RerunsVanta · PHONE SCREEN
  • Course ScheduleByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
  • Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
  • All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Dependency-Aware Task SchedulerRippling · PHONE SCREENRippling interview questions
  • Build Packages as Dependencies Are DiscoveredMongoDB · PHONE SCREENMongoDB interview questions

Snowflake interview experiences

  • Snowflake AI Coding Interview, Experienced Hires: 3 Experiences
All Snowflake interview questionsSee Snowflake hiring insights
Saved
class Solution {
    public int[] findOrder(int n, int[][] prerequisites) {
        // Write your code here.
    }

}
Checking account…