Remove the Most Links While Keeping Two Carrier Networks Connected
Last updated: November 29, 2026
Quick Overview
A delivery network has links usable only by carrier A, only by carrier B, or by both; return the maximum number of links you can remove such that both carriers can still reach every node, or -1 if that is already impossible.
Amazon
November 29, 20260
0
0 solved
A delivery network has links usable only by carrier A, only by carrier B, or by both; return the maximum number of links you can remove such that both carriers can still reach every node, or -1 if that is already impossible.
Amazon Logistics mixes its own delivery network with partner carriers, and shared infrastructure is the most valuable. The interviewer wants a greedy union find that keeps shared links first and proves why that order maximizes removals.
What the Interviewer Expects
- Use two DSUs, one per carrier
- Process shared links first, keeping any that helps either DSU
- Then add carrier-only links to their own DSU
- Check both DSUs end with one component
- Answer is total links minus kept links
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
- Why must shared links come first?
- What if links had costs and you wanted the cheapest kept set?
- How does this relate to two spanning trees sharing edges?
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 nodes labeled 1..n and links (kind, u, v) with kind 1 = A only, 2 = B only, 3 = both. Output: max removable count, or -1 if either carrier cannot connect all nodes using every link....
High-Level Design
Each carrier needs a spanning tree of n - 1 links. A shared link can count toward both trees at once, so it is never worse to use a shared link than a carrier-only link for the same connection.
Greed...