Traveling the Graphs
Updated
Problem statement
Parse an undirected weighted graph and one route request.
edgeLinecontains one or more edge tokens[A,B,5], separated by exactly one space.routeLinehas the formA->D,10: start node, destination node, and maximum allowed travel time.- Node names are single uppercase letters, and weights and the limit are unsigned decimal integers.
Return the unique shortest route as uppercase node names joined by -> when its total distance is at most the limit.
Otherwise return the first applicable error by priority:
E1: input syntax error.E2: logical input error: a duplicate undirected edge, a self-edge, an undefined endpoint, a disconnected graph, or more than one shortest route.E3: the unique shortest route exceeds the allowed travel time.
Function
travelingTheGraphs(edgeLine: String, routeLine: String) → StringExamples
Example 1
edgeLine = "[A,B,3] [B,C,5] [C,D,2]"routeLine = "A->D,10"return = "A->B->C->D"The only route from A to D has total distance 10, exactly the limit.
Example 2
edgeLine = "[A,B,3] [A,C,7] [C,D,2] [B,C,5]"routeLine = "A->D,10"return = "A->C->D"A->C->D has distance 9, shorter than the alternative through B.
Example 3
edgeLine = "[A,B,5] [A,C,2] [B,C,4] [B,D,6] [C,B,7]"routeLine = "A->C,10"return = "E2"[C,B,7] duplicates the already defined undirected edge [B,C,4].
Constraints
- Every node is one of
AthroughZ. 0 <= edgeWeight, maximumTravelTime <= 10^9- The edge line contains at most
325tokens. - No leading, trailing, or repeated whitespace is valid.
More Optiver Online Assessment questions
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
- Fastest Average Speed over a Rolling KilometerOA · Seen Sep 2026
- Multi-Level Inventory Storage SystemOA · PHONE SCREEN · Seen Aug 2026
- Proportional Momentum Investment StatisticsOA · Seen Aug 2026
- Count Bounded Share TransactionsOA · Seen Aug 2026
- Count Ordered Size SequencesOA · Seen Aug 2026
- Profitable Currency Exchange CycleOA · Seen Aug 2026
- Construct Binary Tree S-ExpressionOA · Seen Jul 2026
Similar questions at other companies
- Sum Dynamic Weighted Tree DistancesJuspay · OA
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Spreadsheet Formula EvaluatorPika · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Resolve Variable Equations with Dependency ErrorsMotive · PHONE SCREENMotive interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Job Runner Chain ReportsHeadlands Technologies · OAHeadlands Technologies interview questions
- Minimum Fuel Cost Between CitiesMicrosoft · OAMicrosoft interview questions