Dsa
Stack
Medium
The Load-Bearing Block Tower
A robot builds a tower by stacking blocks. Each block has a weight (W) and a load-capacity (C). The load-capacity is the maximum total weight that can be placed *on top* of that block. When the robot adds a new block, it checks if the new weight violates the capacity of *any* block already in the stack. If the new block exceeds a block's capacity, the lowest such failing block and everything above it is immediately removed from the stack. Then, the robot tries to place the new block on the remaining stack. This process repeats until the block fits or the stack is empty. Input is a series of lines, each with 'W C'. Output the weights of the blocks in the final tower.
Example:
10 100
20 10
5 50
Input with 10 100 then 20 10 then 5 50. Block 2 (20, 10) can't hold 5. So block 2 is removed. Block 1 (10, 100) can hold 5. Final weights: 10 5.
Key concepts
stackmonotonic-stacksimulation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now