Lexicographically Smallest Topological Order
Updated
Problem statement
Order the vertices of a directed graph so every edge goes from an earlier vertex to a later one. For this exercise, assume vertices supplies all unique vertex labels, including isolated vertices. Each pair [u,v] in edges requires u to appear before v. Every endpoint belongs to the supplied vertices.
For this exercise, assume repeated identical pairs represent one dependency. Return every supplied label exactly once in the lexicographically smallest valid order: at the first differing position, the smaller lowercase string wins. If one label is the beginning of another, put the shorter label first.
For this exercise, assume any directed cycle, including a self-loop, makes a complete order impossible; return an empty array. An empty graph also returns an empty array. Do not return a partial order or modify either input.
Function
topologicalOrder(vertices: String[], edges: String[][]) → String[]Examples
Example 1
vertices = ["delta","alpha","charlie","bravo"]edges = [["alpha","charlie"],["bravo","charlie"],["charlie","delta"]]return = ["alpha","bravo","charlie","delta"]Both alpha and bravo are initially available. Choosing alpha first gives the smallest complete order.
Example 2
vertices = ["a","b","z"]edges = [["a","b"],["b","a"]]return = []The a/b cycle prevents a complete order. Do not return the available isolated z as a partial answer.
Example 3
vertices = ["d","c","b","a"]edges = [["c","b"]]return = ["a","c","b","d"]After a and c, b becomes available and must precede the already available d.
Constraints
- For this exercise, assume
0 <= vertices.length <= 100and0 <= edges.length <= 500. - Every unique vertex label contains
1through10lowercase English letters. - Each edge has exactly two labels from
vertices. An empty vertex list has no edges. - Duplicate pairs and self-loops are allowed.
More Coinbase interview questions
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Feb 2022
- Longest Common Contiguous SubarrayPHONE SCREEN · Seen Nov 2021
- Justify Text to a Fixed WidthPHONE SCREEN · Seen Jun 2021
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Sliding-Window Averages as Reduced FractionsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Data Pipeline Steps with Search and Run OrderRippling · PHONE SCREENRippling interview questions
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions