Implement thread-safe blocking queue
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates competency in concurrent programming, synchronization primitives, and implementation of thread-safe bounded blocking queues within the Coding & Algorithms domain.
Constraints
- 0 <= capacity <= 100000
- 1 <= len(ops) <= 200000
- Each op is exactly "ENQ t v" or "DEQ t"
- 0 <= t <= 1e9
- -1e9 <= v <= 1e9
- Operation times are non-decreasing; input order breaks ties at equal times
- All operations complete by the end (no thread remains blocked)
Hints
- Maintain three FIFO queues: the buffer, waiting enqueues, and waiting dequeues.
- On ENQ: first try to match a waiting DEQ; otherwise push to buffer if space, else enqueue to waiting ENQ queue.
- On DEQ: pop from buffer if available; after popping, wake exactly one waiting ENQ if space exists. If buffer empty and a waiting ENQ exists (capacity 0), pair with it immediately.
- Preserve input order for operations at the same time and for serving waiters (FIFO).