Multi-threaded Queue Enqueue
Filed under Amazon
Free read 1 of 3Define a Node class and a Queue class with head/tail pointers.
The question
Implement a thread-safe Enqueue operation for a queue using a linked list, ensuring atomic updates to the head and tail pointers.
Constraints
- Must handle concurrent access
- Minimize lock contention
- Use standard threading primitives
Approach
Define a Node class and a Queue class with head/tail pointers.
Use a ReentrantLock or Mutex to protect the tail pointer and the next pointer of the last node.
Create new node, lock the queue, update tail.next, update tail, unlock.
Handle the case where the queue is initially empty (head and tail are both updated).
Tips
- What they are testing: Focuses on race conditions during pointer updates.
- Common mistake: Holding a lock for too long or failing to handle the empty queue case correctly.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.