Simulate a Printer Queue with Priority Levels
Stacks & QueuesAuto-graded · 15 test casesLast updated
Tests queue simulation by processing print jobs where higher-priority jobs preempt lower-priority ones, requiring candidates to track position and reorder the queue correctly.
Printer Queue Simulation
A shared office printer processes jobs using the following rules:
- The printer inspects the job at the front of the queue.
- If any job remaining in the queue (including the front job itself) has a strictly higher priority, the front job is moved to the back of the queue.
- Otherwise, the front job is printed (removed from the queue).
- This repeats until the queue is empty.
Each print job has an id (unique non-negative integer) and a priority (integer 1–10, where 10 is highest).
You are given a list of jobs as [id, priority] pairs in their initial queue order (index 0 is the front), and a target job id.
Return the 1-based position in the print order at which the job with the given target_id is printed.
Constraints
1 ≤ len(jobs) ≤ 2000- Each job is represented as a two-element list
[id, priority]. 0 ≤ id ≤ 10^6; all ids are unique.1 ≤ priority ≤ 10target_idis guaranteed to be the id of one of the jobs in the list.- The function must return the exact 1-based print position of the target job.
Examples
Example 1
Input: printer_queue_position([[1, 3], [2, 5], [3, 3], [4, 1]], 1)
Output: 3
Example 2
Input: printer_queue_position([[10, 2], [20, 2], [30, 2]], 20)
Output: 2
Example 3
Input: printer_queue_position([[7, 1], [8, 10], [9, 1]], 7)
Output: 3
Hints
Hint 1
Use a collections.deque so you can cheaply pop from the front and append to the back.
Hint 2
To efficiently check 'is there any job with higher priority still in the queue?', maintain a sorted structure or frequency count of current priorities.
Hint 3
Increment a counter each time a job is actually printed; when the printed job's id equals target_id, return that counter.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The printer rule is: if any job in the queue has strictly higher priority than the front job, the front job is demoted to the back. Otherwise, the front job is printed. This is a classic queue simulation problem.
Algorithm
- Load all jobs into a
dequefor O(1) front-pops and back-appends. - Maintain a priority frequency counter (
collections.Counter) so that finding the current maximum priority takes O(1) amortized time viamax(freq)on a dict that has at most 10 keys. - Each iteration:
- Peek at the front job's priority.
- If it equals the current maximum, print it: pop from the front, decrement its priority in the counter, increment
print_order. Ifjob_id == target_id, returnprint_order. - Otherwise, rotate it to the back: pop from the front and append to the back (no counter update needed since the job is still in the queue).
Correctness
Because we always print the job at the front whose priority equals the current maximum in the queue, and we never skip a job — only defer it — every job is eventually printed exactly once. The target_id is guaranteed to be present, so the function always returns before the loop ends.
Complexity
- Time: O(n · P) where n is the number of jobs and P ≤ 10 is the number of distinct priority levels. In the worst case (many priority levels, many deferrals), each job may cycle around the queue multiple times, but each "round" eliminates at least one job, giving O(n²) worst-case. With n ≤ 2,000 this is at most ~4,000,000 operations — well within limits.
- Space: O(n) for the deque and O(P) = O(10) = O(1) for the counter.
from typing import List
from collections import deque, Counter
def printer_queue_position(jobs: List[List[int]], target_id: int) -> int:
queue = deque(jobs)
freq = Counter(p for _, p in jobs)
print_order = 0
while queue:
job_id, priority = queue[0]
max_priority = max(freq)
if priority < max_priority:
# A higher-priority job exists; move this one to the back
queue.popleft()
queue.append([job_id, priority])
else:
# Print this job
queue.popleft()
freq[priority] -= 1
if freq[priority] == 0:
del freq[priority]
print_order += 1
if job_id == target_id:
return print_order
return -1 # unreachable given the guarantee