Uber · Question 33 · Queues
Implement Circular Queue
Problem
Design a circular queue with fixed capacity supporting enqueue, dequeue, front, rear, isEmpty, and isFull operations.
Approach
Use a fixed-size list, a head pointer, and a count of elements. Tail is computed via modular arithmetic.
Time O(1)Space O(k)
How it works
- 1Track head, count and capacity; derive tail by modulo.
- 2Enqueue at the tail unless full; dequeue advances head.
- 3Front/Rear read by position; full/empty compare counts.
Sample input / output
Input: 3 9 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; 0 <= value <= 1000; <= 3000 calls.
Solution — read it, then walk it
Time O(1)Space O(k)Loops 1Branches 6Returns 9source · Python
Scroll sideways for wide charts — the view follows the active step.
- 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