FastPrepLexicographically Smallest Course Order
Problems/Lexicographically Smallest Course Order

Lexicographically Smallest Course Order

LinkedIn logoLinkedIn● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names topological sorting and a follow-up about node distance using DFS. Our version is about 80% the same as the reported question. We keep the rule of using the smallest label first. We chose the shortest distance on a directed graph, how to handle cycles, and the paired result.

Courses are labeled from 0 to numCourses - 1. Each pair [course, prerequisite] is a directed edge from the prerequisite to the course.

Return the lexicographically smallest complete course order. Whenever several courses are available, choose the smallest label.

Follow-up: use depth-first search to find the distance from source to target. For this exercise, assume distance means the smallest number of directed edges. In an acyclic graph, return 0 for the same node and -1 when the target is unreachable.

For this exercise, assume the paired result is [order, [distance]]. If any part of the graph has a cycle, return [[], [-1]]: a complete order and its follow-up result are unavailable. Keep the inputs unchanged.

Function

courseOrder(numCourses: int, prerequisites: int[][], source: int, target: int) → int[][]

Examples

Example 1

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

The order is [0,1,2,3]. Both 0->1->3 and 0->2->3 use two edges, so the shortest distance is 2.

Example 2

numCourses = 2prerequisites = [[0,1],[1,0]]source = 0target = 1return = [[],[-1]]

The two courses form a cycle. There is no complete order, so return an empty first row and the unavailable-distance row [-1].

Example 3

numCourses = 3prerequisites = [[1,0]]source = 2target = 1return = [[0,1,2],[-1]]

The smallest complete order is [0,1,2]. Node 2 has no outgoing edge, so it cannot reach node 1 and the distance is -1.

Constraints

  • 1 <= numCourses <= 200000.
  • 0 <= prerequisites.length <= 300000.
  • Every prerequisite pair has exactly two valid course labels, each between 0 and numCourses - 1.
  • No prerequisite pair is duplicated.
  • 0 <= source, target < numCourses.

More LinkedIn Onsite Interview questions

  • Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
  • Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
  • Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
  • Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
  • Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
  • Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
  • LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
  • K Closest Values in a Binary Search TreeONSITE INTERVIEW · Seen Oct 2026
  • Unique Single-Use Sum CombinationsONSITE INTERVIEW · Seen Oct 2026
  • Maximum Consecutive Ones in a Circular ArrayONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • ETL Pipeline SchedulerUnity · ONSITE INTERVIEW
  • Microservice Deployment OrderGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
  • Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Course Schedule IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Task Order with Smallest Ready Task FirstExpedia · ONSITE INTERVIEWExpedia interview questions

LinkedIn interview experiences

  • LinkedIn Staff AI Engineer Phone Screens: 1 Experience
  • LinkedIn Experienced MLE Onsite: 1 Experience
  • LinkedIn Coding with AI Interview: 4 Experiences
  • LinkedIn Experienced Infrastructure SWE Onsite: 1 Experience
All LinkedIn interview questionsSee LinkedIn hiring insights
Saved
class Solution {
    public int[][] courseOrder(int numCourses, int[][] prerequisites, int source, int target) {
        // Write your code here.
    }
}
Checking account…