← PayPal bank

PayPal · Question 55 · Queues

Design Circular Queue

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

Problem

Implement a circular queue with fixed size.

Approach

Use an array and two pointers for front and rear, modulo arithmetic for wrap-around.

Time O(1)Space O(k)

How it works

  1. 1Rear advances on enqueue, front on dequeue, both modulo capacity.
  2. 2Size 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:
[true, true, true, false, 3, true, true, true, 4]

Constraints

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

Solution — read it, then walk it

Time O(1)Space O(k)Branches 2Returns 2source · Java
Scroll sideways for wide charts — the view follows the active step.
STARTimport java.util.*DEFINE class MainDEFINE classMyCircularQueueDEFINE int[] queueDEFINE int frontDEFINE int rearDEFINE int sizeDEFINE int capacityDEFINE MyCircularQueue(intk)capacity = kqueue = new int[capacity]front = 0rear = -1size = 0DEFINE boolean enQueue(intvalue)if (isFull()) returnfalserear = (rear + 1) %capacityqueue[rear] = valuesize++RETURN trueDEFINE boolean deQueue()if (isEmpty()) returnfalsefront = (front + 1) %capacitysize--RETURN trueDEFINE int Front() { returnisEmpty() ? -1 :queue[front]; }DEFINE int Rear() { returnisEmpty() ? -1 :queue[rear]; }DEFINE boolean isEmpty() {return size == 0; }DEFINE boolean isFull() {return size == capacity; }DEFINE static voidmain(String[] args)MyCircularQueue q = newMyCircularQueue(3)System.out.println("[" +q.enQueue(1) + ", " +q.enQueue(2) + ", " …END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/33

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