← PayPal bank

PayPal · Question 50 · Queues

Design Circular Deque

QueuesJavacode⏱ O(1)💾 O(k)
Product-based

Problem

Design a circular deque supporting insertion, deletion, and access from both ends.

Approach

Use an array with front and rear pointers to simulate the circular deque.

Time O(1)Space O(k)

How it works

  1. 1Front moves backward, rear moves forward, both modulo capacity.
  2. 2Size 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:
[true, true, true, false, 2, true, true, true, 4]

Constraints

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

Solution — read it, then walk it

Time O(1)Space O(k)Branches 6Returns 6source · Java
Scroll sideways for wide charts — the view follows the active step.
STARTimport java.util.*DEFINE class MainDEFINE classMyCircularDequeDEFINE int[] arrDEFINE int frontDEFINE int rearDEFINE int sizeDEFINE int capacityDEFINE MyCircularDeque(intk)capacity = karr = new int[capacity]front = 0rear = 0size = 0DEFINE booleaninsertFront(int value)if (isFull()) returnfalsefront = (front - 1 +capacity) % capacityarr[front] = valuesize++RETURN trueDEFINE booleaninsertLast(int value)if (isFull()) returnfalsearr[rear] = valuerear = (rear + 1) %capacitysize++RETURN trueDEFINE booleandeleteFront()if (isEmpty()) returnfalsefront = (front + 1) %capacitysize--RETURN trueDEFINE boolean deleteLast()if (isEmpty()) returnfalserear = (rear - 1 +capacity) % capacitysize--RETURN trueDEFINE int getFront()if (isEmpty()) return-1RETURN arr[front]DEFINE int getRear()if (isEmpty()) return-1RETURN arr[(rear - 1 +capacity) % capacity]DEFINE boolean isEmpty() {return size == 0; }DEFINE boolean isFull() {return size == capacity; }DEFINE static voidmain(String[] args)
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/46

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