← 返回 atlassian 的题目列表Cart Routes Origin Destinations
类型:qbank
Given directed route pairs for autonomous delivery carts in a robot factory, identify every starting location and all possible ending destinations reachable from each start.
Requirements
Input is a list of directed pairs representing cart route steps.
The graph is directed downward from origins to destinations.
Identify all start locations.
For each start, return the collection of all possible ending locations reachable from it.
N is the number of pairs in the input.
Examples
paths = [
['B', 'K'],
['C', 'K'],
['E', 'L'],
['F', 'G'],
['J', 'M'],
['E', 'F'],
['C', 'G'],
['A', 'B'],
['A', 'C'],
['G', 'H'],
['G', 'I']
]
expected output:
[
'A': ['K', 'H', 'I'],
'E': ['H', 'L', 'I'],
'J': ['M']
]
Notes
Starts are nodes with no incoming edges; destinations are terminal reachable leaves.
Preserve output determinism if the interviewer asks for a specific ordering.
Preparation
Implement graph construction with incoming-degree tracking.
Add memoized DFS from each node to avoid recomputing destination sets.