方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build a request queue with priority routing

Implement an in-memory PriorityInferenceQueue that:

  1. Accepts inference requests with a priority field: "high", "normal", or "low"
  2. Always dequeues high-priority requests before normal, and normal before low
  3. Returns None from dequeue() if the queue is empty
  4. Exposes a depth() method returning a dict with counts per priority level and a total key
  5. Is safe for use with asyncio (use asyncio.Queue or equivalent, not threading primitives)
import asyncio
from dataclasses import dataclass

@dataclass
class InferenceRequest:
    request_id: str
    messages: list[dict]
    priority: str  # "high", "normal", "low"

class PriorityInferenceQueue:
    def __init__(self):
        # TODO: initialize separate queues per priority level
        pass

    async def enqueue(self, request: InferenceRequest) -> None:
        # TODO: add to the correct priority queue
        raise NotImplementedError

    async def dequeue(self) -> InferenceRequest | None:
        # TODO: return highest-priority item, or None if empty
        raise NotImplementedError

    def depth(self) -> dict:
        # TODO: return {"high": n, "normal": n, "low": n, "total": n}
        raise NotImplementedError

Deployment / medium / Step 2 of 6

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Success criteria

Make the solution explicit, debuggable, and easy to explain.

Review checklist

Review where the boundary is, what gets validated, and what would be hard to debug later.

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.