Check Whether Rack Network Links Form a Single Tree
Last updated: November 29, 2026
Quick Overview
Given n switches labeled 0 to n-1 and a list of undirected cable links, return True if the links form a valid tree: every switch connected and no cycles.
Meta
November 29, 20260
0
0 solved
Given n switches labeled 0 to n-1 and a list of undirected cable links, return True if the links form a valid tree: every switch connected and no cycles.
Validating a tree topology is a warm-up in the style of a Meta phone screen, framed around checking a data center rack's cabling plan before it is deployed. The interviewer wants the edge-count shortcut plus a connectivity check, and checks that you can state why both conditions together are sufficient.
What the Interviewer Expects
- States that a tree on n nodes has exactly n-1 edges
- Checks connectivity with BFS, DFS, or union-find
- Explains why n-1 edges plus connected implies no cycle
- Handles n = 1 with no edges
- Rejects duplicate edges and self-loops correctly
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 report the redundant link that creates a cycle?
- How would you find the minimum links to add to make it connected?
- What changes for a directed rooted tree?
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 and links as [u, v] pairs. Output: True if the undirected graph is a tree. n up to 2000, links up to 5000....
High-Level Design
A graph is a tree exactly when it is connected and has n-1 edges. So first reject unless len(links) == n - 1. Then check connectivity with one BFS from node 0 and confirm it reaches all n nodes. Wit...