Thread-safe task queue on a linked list
I was asked this
No reports yet. Sign in to say where you were asked it.
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 atcapacity(if one is set) and raisesFullon timeout. -
get(timeout=None)blocks while the queue is empty and raisesEmptyon timeout. -
close()makes laterputcalls raiseClosed; consumers drain what is left, thengetraisesClosed. 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=0a non-blocking try, and is a negative timeout an error? - After
close(), should a producer blocked inputwake and raiseClosed? - 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
Nonebe a task value?
What a strong answer covers
- The races in the single-threaded version: two
putcalls both read the oldtailand one node is lost;getandputon a one-element list both touchheadandtail;headread and updated by two consumers delivers one task twice. - One lock guarding
head,tailand the size, with two condition variables on it,not_emptyandnot_full. - Waits in a
whileloop 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 callingnotify_allon both conditions, so blocked producers raiseClosedand 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
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
