Find the Last Package Left After Crushing the Two Heaviest
Last updated: November 29, 2026
Quick Overview
A compactor repeatedly takes the two heaviest packages. Equal weights are both destroyed; otherwise the lighter is destroyed and the heavier is reduced by the lighter's weight. Return the weight of the last remaining package, or 0 if none remain.
Amazon
November 29, 20260
0
0 solved
A compactor repeatedly takes the two heaviest packages. Equal weights are both destroyed; otherwise the lighter is destroyed and the heavier is reduced by the lighter's weight. Return the weight of the last remaining package, or 0 if none remain.
A short heap item typical of Amazon's online assessment, checking you can simulate with a max-heap rather than re-sorting each round. The fulfillment framing is a recycling compactor in a returns center.
What the Interviewer Expects
- Choose a max-heap since you repeatedly need the two largest
- Know Python's heapq is a min-heap and negate values
- Push the difference back only when it is non-zero
- Return 0 when the heap empties
- Explain 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 the goal were to minimize the final weight by choosing pairs freely?
- How would you handle a stream of new packages arriving between rounds?
- How does re-sorting each round compare in complexity?
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 weights. Output: integer weight of the final package or 0. Each round removes the two heaviest; if they differ, their difference is reinserted....
High-Level Design
Build a max-heap in O(n) with heapify on negated values. While at least two items remain, pop the two largest y >= x. If y > x, push y - x back. Each round removes at least one package, so there are a...