Dsa
Queue
Easy
The Fair-Share Printer
A shared office printer uses a round-robin scheduling algorithm to ensure fairness. Each user can print up to K pages at a time. If a user's job is longer than K pages, the first K pages are printed, and the remainder of the job is sent to the back of the queue. The printer processes jobs in the order they appear in the queue. Given K and a list of (Name, TotalPages) pairs, output the names of the users in the order their entire jobs are completed.
Example:
Input: 3 3 Alice 5 Bob 2 Charlie 4
Output: Bob Alice Charlie
Explanation:
- Queue: [Alice(5), Bob(2), Charlie(4)]
- Alice prints 3 pages, 2 left, goes to back. Queue: [Bob(2), Charlie(4), Alice(2)]
- Bob prints 2 pages, finished. Output: Bob. Queue: [Charlie(4), Alice(2)]
- Charlie prints 3 pages, 1 left, goes to back. Queue: [Alice(2), Charlie(1)]
- Alice prints 2 pages, finished. Output: Bob Alice. Queue: [Charlie(1)]
- Charlie prints 1 page, finished. Output: Bob Alice Charlie.
Key concepts
queuesimulationround-robin
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