Design a Task Queue
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Practice workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Practice workspace
Problem
Implement TaskQueue. enqueue(name, priority) appends a task. peek() returns the next task name or None. dequeue() removes and returns the next task name or None. prioritize() returns every queued task as [name, priority], ordered by descending priority and then ascending name, without changing FIFO order.
Starter code
from collections import deque
class TaskQueue:
def __init__(self):
pass
def enqueue(self, name, priority):
pass
def peek(self):
pass
def dequeue(self):
pass
def prioritize(self):
passTest cases
fifo-and-priority-view
{
"operations": [
"TaskQueue",
"enqueue",
"enqueue",
"peek",
"prioritize",
"dequeue",
"peek"
],
"arguments": [
[],
[
"write-tests",
2
],
[
"fix-production",
9
],
[],
[],
[],
[]
]
}Expected: [null,null,null,"write-tests",[["fix-production",9],["write-tests",2]],"write-tests","fix-production"]
empty-queue
{
"operations": [
"TaskQueue",
"peek",
"dequeue",
"prioritize"
],
"arguments": [
[],
[],
[],
[]
]
}Expected: [null,null,null,[]]
Wizard outline
- Step 1: Establish isolated empty state
Create one deque per instance and return None for empty peek and dequeue. State ownership and empty behavior must be correct before mutation methods are added.
- Step 2: Preserve FIFO mutation order
Append tasks, inspect the oldest task, and remove from the front. Queue order is an independent invariant from the later priority view.
- Step 3: Project descending priority without mutation
Return a sorted view while leaving the live FIFO deque unchanged. prioritize is a read operation; sorting a copy prevents it from corrupting later dequeues.
- Step 4: Break priority ties by name
Complete the deterministic priority view for equal priorities. A secondary alphabetical key makes output independent of enqueue order while FIFO state remains untouched.
Footguns and prerequisites
- Sorting the internal queue changes which task dequeue returns next.
- Empty peek and dequeue operations must return None instead of raising an indexing error.
- data classes and structured data
Reviewed references
Recommended approach and implementation
Store FIFO records in a deque. Read or remove only the leftmost record; build the priority view from a sorted copy.
Why it works: Appending and removing only at opposite deque ends preserves insertion order. prioritize never mutates the deque and its compound key applies the required priority and name ordering.
from collections import deque
class TaskQueue:
def __init__(self):
self._tasks = deque()
def enqueue(self, name, priority):
self._tasks.append((name, priority))
def peek(self):
return self._tasks[0][0] if self._tasks else None
def dequeue(self):
return self._tasks.popleft()[0] if self._tasks else None
def prioritize(self):
return [list(task) for task in sorted(self._tasks, key=lambda task: (-task[1], task[0]))]