← TCS bank

TCS · Question 2 · Queues

Circular Queue Implementation

QueuesC++code⏱ O(1)💾 O(n)
Service-based

Problem

Implement a circular queue using an array.

Approach

Front and rear wrap with modulo; count tracks fullness.

Time O(1)Space O(n)

How it works

  1. 1Rear advances on enqueue, front on dequeue, both modulo capacity.
  2. 2Count alone answers empty/full.

Sample input / output

Input:
k = 3, ops = [enQueue(1), enQueue(2), enQueue(3), enQueue(4), Rear(), isFull(), deQueue(), enQueue(4), Rear()]
Output:
[1, 1, 1, 0, 3, 1, 1, 1, 4]

Constraints

1 <= k <= 1000.

Solution — read it, then walk it

Time O(1)Space O(n)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <vector>using namespace stdCLASS CircularQueuevector<int> dataDEFINE front, rear, count,capacityDEFINECircularQueue(int k) :data(k), front(0),rear(-1), count(0)…bool enQueue(int value)bool deQueue()DEFINE Front() { returnisEmpty() ? -1 :data[front]; }DEFINE Rear() { returnisEmpty() ? -1 :data[rear]; }bool isEmpty() { returncount == 0; }bool isFull() { returncount == capacity; }DEFINE main()CircularQueue q(3)cout << "[" << q.enQueue(1)<< ", " << q.enQueue(2) <<", " << q.enQueue(3)RETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/19

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits