Queues and deques
21 questions in 6 subtopics.
PriorityQueue
How isjava.util.PriorityQueue implemented, what are the costs of its main operations, and what does its iterator return?Write a method that returns the 10 largest values from an Iterable<Long> of hundreds of millions of elements without sorting all of them. Explain the complexity.A job scheduler stores Job objects in a PriorityQueue ordered by job.priority. An admin endpoint changes job.priority for queued jobs. Afterwards jobs run in an order that matches neither the old nor the new priorities. Explain and fix.Many teams submit jobs to a shared processing platform with priorities, and each worker instance keeps a local PriorityBlockingQueue. Low-priority tenants complain their jobs never run during busy periods. Design a fairer approach.ArrayDeque
Why does the Javadoc recommendArrayDeque over Stack and LinkedList for stack and queue use?A breadth-first traversal over a graph with millions of nodes uses LinkedList as the queue and shows heavy GC. Would you switch to ArrayDeque, and what else would you consider?BlockingQueue implementations
CompareArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue and PriorityBlockingQueue, and explain the difference between put/take, offer/poll and add/remove.Write a producer-consumer pair using a BlockingQueue where the producer reads lines from a file and two consumers process them, and the consumers stop cleanly when the file is done.A service uses Executors.newFixedThreadPool(20) to process incoming events. During a downstream slowdown, heap usage climbs until OutOfMemoryError, and event latency was minutes before the crash. Explain and redesign.Several teams use in-process BlockingQueues to buffer work between request handling and background processing. After a few incidents where deploys lost queued work, you are asked to set guidance on in-memory queues versus a message broker. What would you decide?Java's queue APIs grew from Vector and Stack, to Queue and Deque (5 and 6), to the BlockingQueue family, to Flow (9), which follows Reactive Streams with demand signalling. Why did demand-based backpressure appear, and what does it mean for designing flows between services?Some engineers argue that every queue in your systems, in-process or between services, must be bounded and must have an explicit overload behaviour. Others say unbounded queues are simpler and memory is cheap. What position would you take for the organisation and why?DelayQueue
How doesDelayQueue work, and what must an element class implement?A team implements retry with backoff for failed webhook deliveries by putting failed deliveries in a DelayQueue consumed by a worker thread. What are the risks, and when would you choose ScheduledExecutorService or a persistent scheduler instead?Time-based work such as retries, expirations and reminders is implemented differently by every team: DelayQueue, ScheduledExecutorService, cron jobs and database polling. What long-term platform approach would you choose, and what would it cost?TransferQueue
What doesTransferQueue add over BlockingQueue, and how does LinkedTransferQueue differ from SynchronousQueue?A producer must not run ahead of consumers by more than what they are actively taking, but sometimes it must be able to drop work if no consumer is free within 50 ms. Show how you would use LinkedTransferQueue for this.A shared ingestion library used by several teams hands records from producers to worker threads with LinkedTransferQueue.transfer(), so producers block until a worker takes each record. Teams report upstream HTTP timeouts when workers are slow. How would you review and change the library design?Queue interface methods
The Queue interface has add/offer, remove/poll and element/peek. Explain the difference within each pair, and the extra methodsBlockingQueue adds.A worker loop useswhile (true) {
Task t = queue.poll();
if (t != null) process(t);
}
LinkedBlockingQueue and the service uses 100 percent of a CPU core when idle. Fix it and explain shutdown handling.A service uses ConcurrentLinkedQueue and calls queue.size() in a metrics gauge every second; under load CPU rises. It also checks if (!queue.isEmpty()) queue.remove() in several threads and sometimes gets NoSuchElementException. Explain both and fix them.No question matches these choices.