Lexicographically Smallest Dependency Order
Updated
Problem statement
There are n tasks labeled from 0 to n - 1. Each pair [before, after] in dependencies means that before must be completed before after.
Return the lexicographically smallest order that completes every task. If the dependencies contain a cycle, return an empty array.
Function
dependencyOrder(n: int, dependencies: int[][]) → int[]Examples
Example 1
n = 4dependencies = [[0,2],[1,2],[1,3]]return = [0,1,2,3]Tasks 0 and 1 begin ready, so task 0 is chosen first; the remaining choices follow the same smallest-label rule.
Example 2
n = 3dependencies = [[0,1],[1,2],[2,0]]return = []Every task belongs to a cycle, so no complete order exists.
Constraints
1 <= n <= 1000000 <= dependencies.length <= 200000- Every dependency contains two distinct valid task labels.
- No dependency pair is repeated.
More Okta interview questions
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Role-Based Network Access ControlPHONE SCREEN · Seen Aug 2026
- Simple Bank OperationsONSITE INTERVIEW · Seen Oct 2026
- Randomized Collection with Duplicate ValuesONSITE INTERVIEW · Seen Sep 2026
- Top-Down Employee Reporting ForestONSITE INTERVIEW · Seen Sep 2026
- Fair Multi-Tenant CacheONSITE INTERVIEW · Seen Aug 2026
- CD Rental SystemONSITE INTERVIEW · Seen Jun 2026
Similar questions at other companies
- 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
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Order Sources by Their DependenciesRetool · PHONE SCREENRetool interview questions