Print Queue Page Order Simulator
Stacks & QueuesAuto-graded · 17 test casesLast updated
Tests queue simulation by modeling a shared printer that interleaves multi-page jobs across rounds, returning the final page-output order.
Print Queue Page Order Simulator
A small office has a single printer shared by several users. Each user submits a print job, which is a sequence of pages numbered 1 through pages_i for that job. Jobs are submitted in order and placed in a queue.
The printer works in rounds. In each round it visits every job that still has pages remaining, in the order they appear in the queue (front to back), and prints exactly one page from that job. After printing a page, the job stays in the queue in the same relative order (it is not re-enqueued at the back; the queue order is fixed). Jobs with no remaining pages are simply skipped.
The printer keeps running rounds until every job is fully printed.
Your task is to return the list of page labels in the exact order they come out of the printer. Each page label is the string "J{job_index}-P{page_number}" where job_index is the 1-based index of the job in the original submission list, and page_number is the 1-based sequential page number within that job (so the first page of job 1 is "J1-P1", the second page of job 1 is "J1-P2", etc.).
How rounds work
Given jobs with page counts [3, 1, 2]:
- Round 1: print
J1-P1,J2-P1,J3-P1 - Round 2: print
J1-P2, (J2 done),J3-P2 - Round 3: print
J1-P3, (J2 done), (J3 done)
Output: ["J1-P1","J2-P1","J3-P1","J1-P2","J3-P2","J1-P3"]
Constraints
0 <= len(jobs) <= 10001 <= jobs[i] <= 2000for each job in the list- If
jobsis empty, return[]. - Jobs are always processed in their original submission order within each round.
Examples
Example 1
Input: simulate_print_queue([3, 1, 2])
Output: ['J1-P1', 'J2-P1', 'J3-P1', 'J1-P2', 'J3-P2', 'J1-P3']
Example 2
Input: simulate_print_queue([1, 1, 1])
Output: ['J1-P1', 'J2-P1', 'J3-P1']
Example 3
Input: simulate_print_queue([])
Output: []
Hints
Hint 1
Think of the jobs list as a fixed-order queue; in each round you iterate through all jobs once, printing the next unprinted page of each.
Hint 2
Track how many pages have already been printed for each job using a simple integer counter (or index) per job.
Hint 3
The outer loop runs for max(jobs) rounds; the inner loop iterates over all jobs, skipping those that are already finished.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a straightforward queue simulation — the queue is static in structure (jobs never change order), and we simply cycle through it repeatedly, printing one page per job per round.
Key observations
- Fixed order: jobs never move in the queue, so each "round" is just a single linear pass over the jobs list.
- Per-job counter: we track how many pages have been printed for each job. If
printed[i] < jobs[i], the job still has work; we print the next page and increment the counter. - Termination: we stop when the total pages printed equals the total pages across all jobs.
Why it is correct
Each round visits jobs in original submission order. A job is active if and only if its counter is strictly less than its page count. We append the label at the moment of printing, so the output list naturally reflects the true printer order.
Complexity
| Time | O(N × M) where N = number of jobs and M = max(jobs). In the worst case (one huge job, many tiny jobs) this is O(N + M) per outer iteration, but simplifies to O(sum(jobs)) total page-print operations. |
| Space | O(N) extra for the printed array, plus O(sum(jobs)) for the output list. |
Reference solution
from typing import List
def simulate_print_queue(jobs: List[int]) -> List[str]:
if not jobs:
return []
output = []
printed = [0] * len(jobs) # pages printed so far per job
total_pages = sum(jobs)
while sum(printed) < total_pages:
for i, pages in enumerate(jobs):
if printed[i] < pages:
printed[i] += 1
output.append(f"J{i+1}-P{printed[i]}")
return output
A micro-optimisation is to replace sum(printed) < total_pages with a running counter to avoid recomputing the sum each round, but the straightforward version is clear and correct for the given constraints.