Check Whether Two Data Centers Are Connected by Any Link
Last updated: November 29, 2026
Quick Overview
Given n data centers labeled 0 to n-1 and a list of bidirectional network links, decide whether there is any path from a source data center to a destination data center.
0
0
0 solved
Given n data centers labeled 0 to n-1 and a list of bidirectional network links, decide whether there is any path from a source data center to a destination data center.
A basic graph reachability question dressed in Google's global network, where a planner wants to know if two sites can still talk after some links are taken down. The interviewer looks for building an adjacency list, choosing BFS or DFS, and stopping early once the target is found.
What the Interviewer Expects
- Build an adjacency list from the edge list
- Use a visited set to avoid revisiting nodes on cycles
- Exit early when the destination is reached
- Handle source equal to destination
- Mention union-find as an alternative for many queries
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you answer one million reachability queries on a static graph?
- What changes if links can be added over time?
- What if links are removed over time?
- How would you return the actual path?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
n,edgesas pairs(u, v),source,destination. - Links are undirected; there may be cycles and isolated nodes.
- Return
Trueif a path exists. - Single query for now; follow-ups m...
High-Level Design
Build an adjacency list, then BFS from source, marking nodes visited when they are pushed. If we ever push destination, answer True. If the queue empties, the destination lives in another compon...