Lexicographically Smallest Topological Order
Updated
Problem statement
Order the vertices of a directed graph so that every edge goes from an earlier vertex to a later vertex. For this exercise, assume its vertices are numbered 0 through n-1, including vertices that do not appear in any edge. Each pair [source,destination] in edges requires source to appear before destination.
For this exercise, assume repeated identical pairs represent one dependency. Return every vertex exactly once in the lexicographically smallest valid order: at the first differing position, the smaller vertex label wins.
For this exercise, assume a self-loop or any directed cycle makes a complete order impossible; return an empty array in that case. An empty graph also returns an empty array. Do not return a partial order or modify edges.
Function
topologicalOrder(n: int, edges: int[][]) → int[]Examples
Example 1
n = 4edges = [[0,2],[1,2],[2,3]]return = [0,1,2,3]Initially 0 and 1 are available. Choosing the smaller label first gives [0,1,2,3].
Example 2
n = 3edges = [[0,1],[1,0]]return = []The directed cycle prevents a complete topological order even though isolated vertex 2 is available; return an empty array.
Example 3
n = 4edges = [[2,1]]return = [0,2,1,3]Vertices 0, 2 and 3 are initially free. After 0 then 2, vertex 1 becomes free and precedes 3, giving [0,2,1,3].
Constraints
- For this exercise, assume
0 <= n <= 200and0 <= edges.length <= 500. - Every edge has exactly two integers in
[0,n-1]; whenn=0, the edge list is empty. - Duplicate pairs and self-loops are allowed.
More Intuit Online Assessment questions
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
- Pressure-Isolation Shutdown OrderOA · Seen Sep 2026
- Submask Sum QueriesOA · Seen Sep 2026
Similar questions at other companies
- Course Schedule IIChainalysis · OAChainalysis interview questions
- Course Order, Lowest Number FirstNutanix · OANutanix interview questions
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Dependency-Aware Task SchedulerRippling · PHONE SCREENRippling interview questions
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lexicographic Dependency OrderIllumio · PHONE SCREENIllumio interview questions