Circular Queue (Ring Buffer)
Practice this coding question on Coding Gurukul.
Problem statement
A sensor gateway has a fixed number of storage slots for readings waiting to be transmitted. New readings join the back, and transmission removes the oldest reading from the front. Freed slots must be reusable even after the write position reaches the end of the storage array. Your task Maintain an initially empty circular queue of capacity k. ENQUEUE x inserts x and prints true, or prints false without changing the queue when it is full. DEQUEUE removes the front and prints true, or prints false when empty. FRONT and REAR print the corresponding value, or -1 when empty. EMPTY and FULL print lowercase booleans. Every command produces exactly one line. Use a fixed array and wrapping indices, with O(1) work per command; do not use a built-in queue. A rejected operation must leave all stored readings unchanged. Worked example With capacity 2, inserting 8 and 3 fills the queue. Inserting 5 fails. After removing 8, inserting 5 succeeds by reusing a slot. The front is now 3, the rear is 5, and FULL prints true.
Input format
First line: capacity k and command count q. Next q lines: one of ENQUEUE x, DEQUEUE, FRONT, REAR, EMPTY, FULL.
Output format
Print one result per command, in order.
Constraints
1 <= k <= 1000; 1 <= q <= 10000; 0 <= x <= 1000.
Sample input
2 8 ENQUEUE 8 ENQUEUE 3 ENQUEUE 5 DEQUEUE ENQUEUE 5 FRONT REAR FULL
Sample output
true true false true true 3 5 true
Sign in to run or submit a solution in C++, Java or Python.