← Questions

Thread-safe task queue on a linked list

0
AnthropicCodingLast asked Feb 2026

A coding interview question asked in Anthropic's coding round, last asked Feb 2026. It tests concurrency, linked list and queues.

A task queue was written for one thread: a singly linked list with head and tail, where put appends at the tail and get removes from the head. Explain what goes wrong when many producer and consumer threads share it, then make it safe:

  • put(value, timeout=None) blocks while the queue is at capacity (if one is set) and raises Full on timeout.
  • get(timeout=None) blocks while the queue is empty and raises Empty on timeout.
  • close() makes later put calls raise Closed; consumers drain what is left, then get raises Closed. Waiting threads must wake up.
  • Every task is delivered exactly once; one producer's tasks come out in the order it put them.
class Empty(Exception): ...
class Full(Exception): ...
class Closed(Exception): ...


class TaskQueue:
    def __init__(self, capacity=None): ...
    def put(self, value, timeout=None): ...
    def get(self, timeout=None): ...
    def close(self): ...
    def __len__(self): ...

Clarifying questions

  • Is timeout=0 a non-blocking try, and is a negative timeout an error?
  • After close(), should a producer blocked in put wake and raise Closed?
  • Does len() need to be exact at the moment it returns, or is a snapshot enough?
  • Should waiting consumers be served in arrival order (fairness), or is any order fine?
  • Can None be a task value?

What a strong answer covers

  • The races in the single-threaded version: two put calls both read the old tail and one node is lost; get and put on a one-element list both touch head and tail; head read and updated by two consumers delivers one task twice.
  • One lock guarding head, tail and the size, with two condition variables on it, not_empty and not_full.
  • Waits in a while loop that rechecks the condition (spurious wake-ups, stolen items), with timeouts computed from a single deadline so repeated wake-ups don't extend them.
  • close() setting a flag and calling notify_all on both conditions, so blocked producers raise Closed and consumers drain the remaining items before raising it.
  • Per-producer order follows from appending under the lock; tests with many producers and consumers checking every item arrives exactly once and in order per producer, plus timeout and close cases.

Follow-ups

  • Make it faster under contention: two locks (one for the head, one for the tail) with a dummy node.
  • A lock-free version with compare-and-swap, and the ABA problem.
  • Add priorities or delayed tasks.
  • Make it work across processes or machines.

Where it's asked

  • Anthropic · Coding · Last asked Feb 202655 min · Live coding on a realistic, multi-part problem, a different one from the screen · No AI

What it tests

concurrency · linked list · queues · python

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding