Quick revision
The short answer
ArrayList.remove(0) shifts every remaining element one place to the left, so each removal from the front costs O(n), and draining a million tasks does about half a trillion element moves. LinkedList avoids the shift but allocates a node per task and has poor memory locality. ArrayDeque, a circular array, gives O(1) add at the tail and remove at the head; if producers and consumers are different threads, a bounded BlockingQueue such as ArrayBlockingQueue or LinkedBlockingQueue is the right choice.
What to remember
In this answer, we saw that ArrayList.remove(0) shifts every remaining element, so draining a million tasks costs about half a trillion copies. LinkedList removes the shift but pays with a node per task and poor locality. ArrayDeque uses a circular array with head and tail indexes, which makes both ends O(1) and stays compact. For producers and consumers on separate threads, we use a bounded BlockingQueue such as ArrayBlockingQueue or LinkedBlockingQueue with an explicit capacity, so that a backlog turns into backpressure instead of an out-of-memory error.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ArrayList.remove(0) shifts every remaining element one place to the left, so each removal from the front costs O(n), and draining a million tasks does about half a trillion element moves. LinkedList avoids the shift but allocates a node per task and has poor memory locality. ArrayDeque, a circular array, gives O(1) add at the tail and remove at the head; if producers and consumers are different threads, a bounded BlockingQueue such as ArrayBlockingQueue or LinkedBlockingQueue is the right choice.
Why this question is asked
Queues are everywhere in backend code: work queues, retry buffers, event batches. Picking the wrong collection for one works perfectly in a test with a hundred items and falls over with a million. Interviewers ask this to see whether we connect an operation to its cost inside the data structure, and whether we notice the concurrency question hidden in the words producers and consumers. In this answer, we will see what remove(0) really does, measure the cost, compare LinkedList and ArrayDeque, and then pick the right queue for multiple threads.
1What ArrayList.remove(0) Really Does
An ArrayList stores its elements in a plain array, with element 0 at the start. An array cannot have a hole at the front, so when we remove element 0, ArrayList copies elements 1 to n-1 one position to the left using System.arraycopy, and then clears the last slot.
Adding at the end is cheap, amortised O(1), because it writes into the next free slot and only occasionally grows the array. Removing from the front is the expensive direction. With a million pending tasks, the first removal moves 999,999 references, the next one 999,998, and so on.
System.arraycopy is very fast per element, but no constant factor survives half a trillion copies. Java 21 added removeFirst() to List through sequenced collections, and it is worth knowing that on ArrayList it does exactly the same shift, so the new method name does not change the cost.
2Why LinkedList Is Not the Answer Either
LinkedList removes from the head in O(1): it just moves the head pointer to the next node. So on paper it fixes the problem. In practice it brings new costs. Every element needs its own Node object with a previous reference, a next reference and an item reference, about 24 bytes per task on top of the task itself, so a million tasks add a million objects for the GC to trace.
The nodes are also scattered around the heap. Walking or removing them touches a new cache line for each node, which is slow compared with walking a contiguous array where one cache line holds 16 references. LinkedList is rarely the fastest choice for anything in modern Java.
3ArrayDeque: a Circular Array
ArrayDeque keeps elements in an array too, but it treats the array as a ring. It tracks a head index and a tail index. Adding at the tail writes into the slot at tail and advances tail; removing from the head reads the slot at head, clears it, and advances head. When an index reaches the end of the array, it wraps around to 0. Nothing is ever shifted.
| Collection | add at tail | remove at head | Per-element overhead |
|---|---|---|---|
ArrayList | O(1) amortised | O(n), shifts all | 4-byte slot |
LinkedList | O(1) | O(1) | about 24-byte node object |
ArrayDeque | O(1) amortised | O(1) | 4-byte slot |
ArrayBlockingQueue | O(1), may block | O(1), may block | 4-byte slot, fixed capacity |
This gives the best of both: constant-time operations at both ends and a compact array that is friendly to the CPU cache, with no per-element objects. The array only grows when it is full, doubling like an ArrayList. Two limits to know: ArrayDeque does not accept null elements, and it is not thread-safe.
4Producers and Consumers on Different Threads
The question says producers and consumers, which usually means different threads. None of the collections above is safe to use from several threads without locking. A plain ArrayDeque shared between threads can lose tasks or corrupt its indexes. For this case the JDK has BlockingQueue implementations, which are thread-safe and let a consumer wait for work and a producer wait for space.
ArrayBlockingQueue is a circular array with a fixed capacity and a single lock. LinkedBlockingQueue uses separate locks for putting and taking, which can give more throughput when producers and consumers are both busy, but it allocates a node per element and, importantly, is unbounded by default, with a capacity of Integer.MAX_VALUE. So we always pass a capacity.
5Why the Bound Matters
A queue that can hold a million pending tasks is already a warning sign. If producers are faster than consumers for a while, an unbounded queue keeps growing until the heap runs out, and every task in it waits longer and longer. A bound turns that slow failure into a fast, visible signal: the producer blocks or gets false from offer, and we can apply backpressure, shed load or scale consumers.
So the full answer is ArrayDeque for a single-threaded queue, and a bounded ArrayBlockingQueue or LinkedBlockingQueue for producer and consumer threads, with the bound sized from how much latency and memory we are willing to accept.
6Follow-up questions
What about ConcurrentLinkedQueue?
It is a lock-free, unbounded, thread-safe queue. It is fast for many producers and consumers but has no blocking operations and no bound, so consumers must poll and producers can grow it without limit. It fits when we never want to block and have another way to limit growth.
Is remove(size() - 1) on an ArrayList also slow?
No. Removing the last element shifts nothing, so it is O(1). That is why an ArrayList works well as a stack, and why ArrayDeque is still preferred for a stack too: it gives the same speed without the misleading index arithmetic.
How would you spot this problem in production?
A CPU profile shows most time in System.arraycopy called from ArrayList.remove, and throughput drops as the queue grows. A quick check is that consumers get slower when the backlog is large, which is the signature of an O(n) operation per item.
7Conclusion
In this answer, we saw that ArrayList.remove(0) shifts every remaining element, so draining a million tasks costs about half a trillion copies. LinkedList removes the shift but pays with a node per task and poor locality. ArrayDeque uses a circular array with head and tail indexes, which makes both ends O(1) and stays compact. For producers and consumers on separate threads, we use a bounded BlockingQueue such as ArrayBlockingQueue or LinkedBlockingQueue with an explicit capacity, so that a backlog turns into backpressure instead of an out-of-memory error.
Open with
The problem is that ArrayList stores elements in an array starting at index 0, so remove(0) has to shift every remaining element one place to the left with System.arraycopy.
Then, if they want more
Each removal is O(n), and draining a million tasks means about half a trillion element copies, so consumers get slower as the backlog grows.
LinkedList would make removal from the head O(1), but it allocates a node of about 24 bytes for every task and scatters them around the heap, so it is heavy on the GC and poor for cache locality. For a single-threaded queue, the right choice is ArrayDeque.
It is a circular array with a head and a tail index, so adding at the tail and polling from the head are both O(1), nothing is shifted, and the storage is compact. It does not allow nulls and is not thread-safe.
Since the question mentions producers and consumers, I would assume different threads, and then I would use a BlockingQueue: ArrayBlockingQueue, a bounded ring buffer with one lock, or LinkedBlockingQueue with an explicit capacity, because by default it is effectively unbounded.
The bound gives backpressure: put blocks or offer fails when consumers fall behind, instead of the queue growing until the heap runs out.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A worker queue holds up to one million pending tasks; producers add at the end and...": https://scrivio-interview-library.netlify.app/java/java-performance/3558265833/