Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement a circular queue

Create a fixed-size circular queue class supporting enqueue, dequeue and access operations.

Python practice15 minStacks & QueuesIntermediateLast updated March 27, 2026

Problem statement

Implement a CircularQueue with a fixed capacity k. Provide operations: enqueue(value) -> bool (True if enqueued, False if full), dequeue() -> value or None (returns removed element or None if empty), front() -> value or None, rear() -> value or None, is_empty() -> bool, is_full() -> bool. Also provide a helper simulate(ops, k) that runs a sequence of operations and returns a list of results corresponding to each operation. Each operation in ops is a tuple: (op_name, value) where value is ignored for non-enqueue ops.

Task

Design and implement a circular buffer with constant-time enqueue/dequeue and index wrap-around.

Examples

Basic enqueue/dequeue

Input

simulate([('enq', 1), ('enq', 2), ('deq', None)], 3)

Output

[True, True, 1]

Two enqueues succeed, dequeue returns the first enqueued value 1.

Input format

A list of operation tuples and an integer capacity k.

Output format

A list of results (booleans for enqueue, values for dequeue/front/rear, booleans for is_empty/is_full).

Constraints

Capacity k is an integer between 1 and 10^4. Number of operations is between 1 and 10^4.

Samples

Sample 1

Input

simulate([('enq',1), ('enq',2), ('enq',3), ('enq',4)], 3)

Output

[True, True, True, False]

Third enqueue fills the queue, fourth fails.