Collide the Two Largest Cache Shards Until One Is Left
Last updated: November 29, 2026
Quick Overview
Each cache shard has a size. Repeatedly take the two largest shards: if equal, both are dropped; otherwise the larger shrinks by the smaller's size. Return the size of the last shard, or 0 if none remain.
0
0
0 solved
Each cache shard has a size. Repeatedly take the two largest shards: if equal, both are dropped; otherwise the larger shrinks by the smaller's size. Return the size of the last shard, or 0 if none remain.
A short online-assessment style warm-up in a cache compaction setting. Google uses questions like this to confirm the candidate knows how to simulate with a max-heap and can reason about termination.
What the Interviewer Expects
- Uses a max-heap (negated values in Python)
- Simulates exactly the stated rule
- Handles the empty and single-shard cases
- Argues the loop terminates
- States O(n log n) time
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 you instead wanted the smallest possible final size?
- Can you do it faster if sizes are small integers?
- How would you return the sequence of collisions?
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: list of positive integer shard sizes.
- Repeat: remove the two largest
a >= b; ifa > b, put backa - b. - Stop when at most one shard remains; return its size or 0....
High-Level Design
The operation always needs the current two largest values, and it inserts a new value afterward. That is the textbook use of a max-heap. Python's heapq is a min-heap, so store negated sizes. Each ro...