KatInterview

Print Queue Page Order Simulator

Stacks & QueuesAuto-graded · 17 test cases
EasyAuto-gradedStacks & Queuesqueuesimulationarrays

Last 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) <= 1000
  • 1 <= jobs[i] <= 2000 for each job in the list
  • If jobs is 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.