Skip to content
BytePatterns

Ring Buffer Deque

MediumStacks & Queues#circular-buffer#design~25m

Problem

Design a double-ended queue whose capacity is fixed when it is created. push_front and push_back add a value and return True, or return False without adding anything when the queue is full; pop_front and pop_back remove and return a value, or return None when the queue is empty. Every operation must take O(1) time, so no stored value may ever be shifted.

Examples

Operations: RingDeque(3), push_back 1, push_back 2, push_front 0, push_back 9
Results:    True, True, True, False
Why:        the fourth push finds all three cells taken
Operations: pop_back, pop_front, pop_front, pop_front   (continuing)
Results:    2, 0, 1, None
Why:        front to back the queue held 0, 1, 2
Operations: RingDeque(1), push_front 5, pop_back
Results:    True, 5
Why:        edge case, with one cell the front and the back are the same slot

Hints

0 / 3

Stuck on the idea rather than the code? Circular Queue covers it.