Remove the Most Links While Keeping Two Networks Fully Connected
Last updated: November 29, 2026
Quick Overview
Offices 1 to n are linked by three kinds of links: corp-only, guest-only, and shared links usable by both networks. Return the maximum number of links you can remove so that both the corp network and the guest network still connect all offices, or -1 if that is impossible even now.
0
0
0 solved
Offices 1 to n are linked by three kinds of links: corp-only, guest-only, and shared links usable by both networks. Return the maximum number of links you can remove so that both the corp network and the guest network still connect all offices, or -1 if that is impossible even now.
Google offices run separate corporate and guest networks over shared fiber, and this onsite question checks greedy reasoning with two union-finds. The key is taking shared links first, since one shared link can replace two dedicated ones, and justifying why that greedy order is optimal.
What the Interviewer Expects
- Uses two separate union-finds, one per network
- Processes shared links first and keeps one if it helps either network
- Then adds dedicated links only where needed
- Verifies both networks are connected at the end
- Explains why shared-first is optimal
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
- What if links have costs and you must maximize the cost removed?
- Support a third network type with its own links.
- Return which links to keep, not just the count removed.
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:
nand list of(type, a, b)with type 1 (corp), 2 (guest), 3 (shared). - Output: max removable links, or -1 if either network cannot be connected....
High-Level Design
Each network needs a spanning tree. A shared link counts toward both trees at once, so it is always at least as good as two dedicated links. Process shared links first in both union-finds and keep eac...