← Infosys bank

Infosys · Question 51 · Queues

Design Circular Deque

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

Problem

Implement a circular deque with operations insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.

Approach

Use a fixed-size array with pointers for front, rear, and count.

Time O(1)Space O(k)

How it works

  1. 1Front moves backward, rear moves forward, both modulo capacity.
  2. 2Count alone answers empty/full.

Sample input / output

Input:
k = 3, ops = [insertLast(1), insertLast(2), insertFront(3), insertFront(4), getRear(), isFull(), deleteLast(), insertFront(4), getFront()]
Output:
[1, 1, 1, 0, 2, 1, 1, 1, 4]

Constraints

1 <= k <= 1000; <= 2000 calls.

Solution — read it, then walk it

Time O(1)Space O(k)Branches 2Returns 3source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <vector>using namespace stdCLASS MyCircularDequevector<int> dqDEFINE frontIdx, rearIdx,capacity, countDEFINEMyCircularDeque(int k)bool insertFront(int value)bool insertLast(int value)bool deleteFront()bool deleteLast()DEFINE getFront()if (isEmpty()) return-1RETURN dq[frontIdx]DEFINE getRear()if (isEmpty()) return-1RETURN dq[(rearIdx - 1 +capacity) % capacity]bool isEmpty() { returncount == 0; }bool isFull() { returncount == capacity; }DEFINE main()MyCircularDeque dq(3)cout << "[" <<dq.insertLast(1) << ", " <<dq.insertLast(2)RETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/25

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