Scrivio Interview library

Queues and deques

21 questions in 6 subtopics.

PriorityQueue

InternalsFoundations8 min read
How is java.util.PriorityQueue implemented, what are the costs of its main operations, and what does its iterator return?
CodingFoundations7 min read
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.
DebuggingIntermediate8 min read
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.
DesignAdvanced7 min read
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

ConceptFoundations7 min read
Why does the Javadoc recommend ArrayDeque over Stack and LinkedList for stack and queue use?
ScenarioIntermediate7 min read
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

ConceptFoundations7 min read
Compare ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue and PriorityBlockingQueue, and explain the difference between put/take, offer/poll and add/remove.
CodingFoundations7 min read
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.
ScenarioIntermediate7 min read
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.
DesignAdvanced7 min read
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?
ConceptPrincipal7 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations7 min read
How does DelayQueue work, and what must an element class implement?
ScenarioIntermediate7 min read
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?
LeadershipPrincipal7 min read
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

ConceptIntermediate7 min read
What does TransferQueue add over BlockingQueue, and how does LinkedTransferQueue differ from SynchronousQueue?
CodingIntermediate7 min read
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.
ScenarioAdvanced7 min read
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

ConceptFoundations6 min read
The Queue interface has add/offer, remove/poll and element/peek. Explain the difference within each pair, and the extra methods BlockingQueue adds.
DebuggingFoundations6 min read
A worker loop uses
while (true) {
Task t = queue.poll();
if (t != null) process(t);
}
on a LinkedBlockingQueue and the service uses 100 percent of a CPU core when idle. Fix it and explain shutdown handling.
ScenarioIntermediate6 min read
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.