Dsa
Queue
Easy
The Weight-Limited Bridge
A bridge has a maximum weight limit W. Cars arrive at the bridge entrance at various times. Every car takes exactly 10 seconds to cross. A car can only enter the bridge if its weight plus the total weight of all cars currently on the bridge does not exceed W. If a car cannot enter, it waits at the front of the queue, blocking all cars behind it, until enough weight clears (i.e., cars finish crossing).
Input Format:
Line 1: Weight limit W
Subsequent lines: Weight and ArrivalTime of each car.
Output Format:
For each car, print the time it *enters* the bridge.
Example:
Input:
10
10 0
5 5
Output:
0
10
Explanation: Car 1 (weight 10) enters at 0. It clears at 10. Car 2 (weight 5) arrives at 5, but the bridge is at capacity (10/10). It must wait until t=10.
Key concepts
queuesimulation
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