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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

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
Max-Heap
Simulation
Greedy
Priority Queue
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list of positive integer shard sizes.
  • Repeat: remove the two largest a >= b; if a > b, put back a - 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...


Submit Your Answer
Markdown supported