Order Sources by Their Dependencies
Updated
Problem statement
You receive unique source names and dependency pairs. A pair [source, prerequisite] means that prerequisite must be processed before source.
Return an ordering containing every source exactly once and satisfying every dependency. If several sources have all their prerequisites completed, choose the name that comes first alphabetically. This gives the smallest valid ordering in lexicographic order.
If any cycle prevents completing all sources, return the one-element array ["IMPOSSIBLE"]. With no sources, return an empty array.
Function
sourceOrder(sources: String[], dependencies: String[][]) → String[]Examples
Example 1
sources = ["app","lib","core","docs"]dependencies = [["app","lib"],["lib","core"]]return = ["core","docs","lib","app"]Every prerequisite must appear first. Choose the lexicographically smallest currently available source, or report a cycle.
Example 2
sources = ["a","b"]dependencies = [["a","b"],["b","a"]]return = ["IMPOSSIBLE"]Every prerequisite must appear first. Choose the lexicographically smallest currently available source, or report a cycle.
Example 3
sources = []dependencies = []return = []Every prerequisite must appear first. Choose the lexicographically smallest currently available source, or report a cycle.
Constraints
0 <= sources.length <= 1000.- Each unique source name has
1to20lowercase English letters. 0 <= dependencies.length <= 5000.- Every pair references supplied names, and dependency pairs are unique. Self-dependencies are allowed and form a cycle.
More Retool interview questions
- Maximum Profit with Exactly One Stock TransactionPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Execute In-Memory SQL QueriesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Maximum Profit with Exactly Two Stock TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Next Consistent Wordle GuessPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
- Wordle FeedbackPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
- Wordle Guess LoopPHONE SCREEN · Seen Jun 2024
- Markov Transition Counts and ProbabilitiesONSITE INTERVIEW · Seen Mar 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
- Dependency-Aware Agent Task SchedulerOpenAI · PHONE SCREENOpenAI interview questions