Skip to main content
ZhimaYuandi

D02 · PREDICT · OPERATE · EXPLAIN YOUR CHOICE

Queue Lab: Process Jobs in Order and Inspect Ring Buffers

One failed check prevents later documents from running, and retrying counts the same consumption twice. Separate job identity, enqueue attempts and processing state.

Back to the practice route →

Make a prediction first

  • Wrap collections.deque in WorkQueue for FIFO. Each enqueued attempt starts at most once and moves from waiting to processing to success or failure. Continue consuming later items after a failure.
  • Explicit retry creates a new attempt ID linked to the earlier failure; it is not the same consumption. Processing is sequential in one process, checking sample records and producing reports without automatic insertion.
  • The separate fixed-capacity ring model must neither overwrite waiting items when full nor invent tasks when empty. Head and tail wrap by capacity. A general-purpose ring container is optional.

This is a deterministic teaching model. State checks inspect model invariants, not your prediction or Python code, and do not establish mastery. Model operation counts, browser time and Python implementation costs are different measures.

Controlled data-structure model

Inspect the model manually

Each action changes a controlled sample. This does not execute student Python, read disks or certify mastery from clicks or model state.

Sequential batch: each step starts or finishes one attempt

One logical job may have multiple attempts. A new retry joins the tail; the next start takes the earliest waiting attempt.

  1. attempt-1 · job-A · Waiting · Start order: — · Finish order: —
  2. attempt-2 · job-B · Waiting · Start order: — · Finish order: —
  3. attempt-3 · job-C · Waiting · Start order: — · Finish order: —

Separate ring-buffer model

R tokens are separate from batch attempts above. Changing capacity clears this ring. Head may equal tail when full or empty; inspect size too.

head=0, tail=0, size=0, capacity=4

  1. [0] ∅ ← head ← tail
  2. [1] ∅
  3. [2] ∅
  4. [3] ∅

A, B and C wait in order; B is preset to fail. The separate ring model is empty. · Current model invariants: Consistent

Operation counts and model scope

Checks FIFO starts, one start per attempt, four-state consistency, retry identity and ring occupancy/bounds; this is not local-code verification.

Enqueued attempts
3
Started
0
Completed
0
Failures
0
New retries
0
Ring enqueues
0
Ring dequeues
0
Waiting
3
Processing
0
Ring size
0

Local deque operations at either end are typically O(1); job execution adds its own cost. Counts cover enqueue, start, completion, failure, retry and ring operations. Browser state snapshots and UI copies do not describe queue algorithm cost.

Counts follow the model definition, not browser elapsed time. Copies for displaying and saving model state are not counted as underlying container operations.

Execution trace (up to 40 recent events)

  1. A, B and C wait in order; B is preset to fail. The separate ring model is empty.

This checks model state only. It does not execute your Python or grade your prediction.

Unchecked or invalidated

Progressive learning hints

Hints are fixed teaching content. Their use is recorded.

Hint 1

Record each enqueue attempt ID. Should retrying a logical job reuse its attempt ID or create another?

Hint 2

Separate starting from finishing. Failure is a terminal state and must not prevent the next waiting item from starting.

Hint 3

Check that start order matches enqueue order and each attempt starts once. Wrap ring indices modulo capacity, using size to distinguish full from empty when head equals tail.

My Python draft

Save and export your draft here. Code is not executed or synced to the starter. Draft, input or strategy edits invalidate earlier model results and self-reports.

Local learning records

Saved only in this browser; export is available. No cross-device sync or proof of mastery. Earlier F01 and six-task records are preserved through migration.

  • D02 opened: —
  • Model check: Unchecked or invalidated
  • Local tests (self-reported): —
  • Highest hint level: 0/3
  • Reference viewed: —
  • Unaided variant (self-reported): —

Implement it in the same Campus Resource Organizer

from collections import deque

class WorkQueue:
    def __init__(self):
        self._waiting = deque()

    def enqueue(self, task):
        raise NotImplementedError

    def step(self, checker=None):
        raise NotImplementedError

An independent snapshot adds only this lab’s fault to the M1 baseline. The M2 starter contains all three labs’ unfinished parts. Keep reference answers separate from your own work.

Download this lab’s starter

python check_environment.py
python -m unittest tests.test_d02 -v
python -m unittest discover -s tests -v

Use Python 3.11+ and its standard library. Extract into a new directory. See the package’s exact intentional-failure list. A passing reference validates materials; you still need to run your own code.

Independent variant

Independently implement challenges.run_print_queue(initial_jobs, during_first=()): fail the first item, append another while processing and continue through every job. Add assertions for FIFO, completeness and continuation after failure. Explain why an undo stack would give the wrong order.

python -m unittest tests.test_m2_variants -v

Replace the skipped placeholder in tests/test_student_counterexamples.py with your own counterexample and explanation. Attempts using hints or references are not recorded as unaided.

Costs and model boundaries

Local deque operations at either end are typically O(1); job execution adds its own cost. Counts cover enqueue, start, completion, failure, retry and ring operations. Browser state snapshots and UI copies do not describe queue algorithm cost.

Reference explanation and answer

View reference explanation (records answer use)

FIFO determines start order, not success. Separating one attempt’s states from a new retry makes duplicate consumption observable. The model advances a head position rather than using array-copy cost to justify efficient dequeue. It makes no semaphore scheduling promise.

Download this lab’s reference (answer) · Version and source digest

Course references and related experiments