Queues & deques: interview questions & scenarios
These are the queue questions I use to test whether you know which end removes, what waits, and what a heap does not promise. Answer it out loud first, then open the model answer, and revisit the lessons when a detail feels fuzzy: Queue, Deque and ArrayDeque, PriorityQueue and binary heaps and blocking and concurrent queues.
What's the difference between offer/poll and add/remove on a queue?
Tests Queue API, failure modes, empty queues
Show answerHide answer
Model answer
The pairs do similar operations but report failure differently. offer(e) tries to insert and returns false if it cannot. add(e) can throw IllegalStateException when insertion is currently impossible, especially for capacity-restricted queues. poll() removes and returns the head, or returns null if the queue is empty. remove() removes and returns the head, but throws NoSuchElementException when empty.
For bounded or uncertain queues, I prefer offer and poll because the failure path is a value I can handle. For code that treats empty as a bug, remove can be acceptable.
How I’d say it in the interview
"Offer and poll are the polite methods: they return false or null when the operation cannot happen. Add and remove are the strict methods: they throw on failure. In real queue-processing code I usually want offer and poll."
Signals depth
Mentioning peek() versus element() too: peek returns null on empty, element throws.
Red flags
"They are exact aliases." They are not, especially once capacity or emptiness matters.
Follow-ups interviewers ask next
What if the queue permits null?
Most modern queue implementations used in interviews, including
ArrayDeque,PriorityQueueandBlockingQueueimplementations, reject null. That keepspoll()returning null as an empty signal.
Why use ArrayDeque instead of Stack or LinkedList for stack and queue work?
Tests Deque, legacy Stack, cache locality
Show answerHide answer
Model answer
ArrayDeque is the default in-memory stack or FIFO queue when you do not need blocking. It stores references in a resizable circular array, so adding and removing at both ends is amortised O(1), with good cache locality.
Stack is a legacy synchronized Vector subclass. Its per-method locking is usually noise, and the API is old. LinkedList can act as a queue, but each element is a separate node with extra references and pointer chasing. It also permits null, which makes queue code harder to reason about.
How I’d say it in the interview
"For a local stack or queue, I reach for ArrayDeque. It gives push, pop, offer and poll at the ends without legacy synchronization and without linked-node overhead. I use LinkedList only when I need its list iterator behavior, not as my default queue."
Signals depth
Separating non-blocking in-memory queues from BlockingQueue, which is the right family for producer-consumer waiting.
Red flags
"Stack is best because it has push and pop." Deque has them too.
Follow-ups interviewers ask next
What if I need to insert in the middle?
Then it is not a queue or stack problem anymore. Revisit whether you need a list, and remember that
LinkedListonly wins if you already hold the node position through aListIterator.
You are implementing a FIFO job scheduler. Which collection do you choose?
Tests FIFO, ArrayDeque, blocking distinction
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
You are designing a task manager where higher-priority tasks run first. Which collection do you choose?
Tests PriorityQueue, comparator, tie-breakers
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does this PriorityQueue code print?
Tests heap order, iteration order, poll order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Debug this max-heap comparator.
Tests comparator overflow, Integer.compare
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Write a bounded producer-consumer queue.
Tests BlockingQueue, backpressure, poison pill
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A service using LinkedBlockingQueue runs out of memory. What do you check?
Tests unbounded queues, capacity, executor defaults
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is SynchronousQueue, and why does a cached thread pool use it?
Tests zero-capacity handoff, ExecutorService, backpressure
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
When would you use DelayQueue?
Tests delayed scheduling, Delayed, priority timing
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why does ArrayDeque reject null?
Tests null sentinel, API design, poll semantics
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal