Web / Apache Commons Collections Interview questions
Explain the internal working of CircularFifoQueue?
CircularFifoQueue<E> is backed by a fixed-size array rather than a linked structure, and it tracks the logical start and end of the queue using index counters that wrap around using modulo arithmetic once they reach the end of the array - hence "circular."
When the queue is full and a new element arrives, instead of throwing an exception or rejecting the insert, the internal start pointer is advanced by one, logically discarding the oldest element, and the new element is written into the freed array slot at the end pointer's position, which itself wraps around to index 0 once it reaches the array's length.
Because there's no per-element node allocation - unlike a LinkedList-backed Deque - both the memory footprint and the cost of adding/removing an element stay flat and predictable regardless of how long the queue has been running, which is exactly the property you want in something like a rolling metrics buffer that never grows unbounded.
More Related questions...