Check connectivity between two subway stations
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This problem evaluates graph connectivity and traversal concepts—specifically reasoning about adjacency representations, path existence, cycles, disconnected components, and missing-node edge cases—within the Coding & Algorithms domain at an implementation-level (medium) abstraction.
Constraints
- 0 <= number of stations in the map <= 100000
- 0 <= total number of listed connections <= 200000
- Station names are strings
- A station may appear in a neighbor list even if it is not a key in the map; such a station has no outgoing connections
Examples
Input: ({'A': ['B', 'C'], 'B': ['D'], 'C': [], 'D': ['E'], 'E': []}, 'A', 'E')
Expected Output: True
Explanation: One valid path is A -> B -> D -> E.
Input: ({'A': ['B'], 'B': ['C'], 'C': ['A', 'D'], 'D': []}, 'A', 'D')
Expected Output: True
Explanation: The graph contains a cycle A -> B -> C -> A, but D is still reachable through A -> B -> C -> D.
Input: ({'A': ['B'], 'B': [], 'C': ['D'], 'D': []}, 'A', 'D')
Expected Output: False
Explanation: A and D are in different disconnected components.
Input: ({'A': ['B'], 'B': ['A']}, 'B', 'B')
Expected Output: True
Explanation: A station is always reachable from itself.
Input: ({'A': ['B'], 'B': []}, 'X', 'B')
Expected Output: False
Explanation: X is not in the map, so it has no outgoing connections and cannot reach B.
Input: ({'A': ['B'], 'B': ['X']}, 'A', 'X')
Expected Output: True
Explanation: X is not a key in the map, but it can still be reached because B connects directly to X.
Input: ({}, 'A', 'B')
Expected Output: False
Explanation: With an empty graph, there are no connections to follow.
Hints
- This is a graph reachability problem. Try exploring from the start station using DFS or BFS.
- Keep a visited set so you do not get stuck in cycles by revisiting the same station repeatedly.