Implement a Queue using a Circular Array

Data Structures
Medium
Microsoft
51.1K views

Implement a Queue data structure using a fixed-size circular array. Handle the wrap-around logic for the head and tail pointers.

Why Interviewers Ask This

Microsoft interviewers ask this to evaluate your mastery of low-level memory management and pointer arithmetic without relying on built-in libraries. They specifically test if you can handle edge cases in a fixed-size buffer, such as distinguishing between an empty and full queue when using modulo arithmetic. This question reveals whether you understand the trade-offs between space efficiency and implementation complexity in performance-critical systems.

How to Answer This Question

1. Clarify requirements immediately: confirm if the array size is fixed, how to handle overflow (throw exception vs return false), and whether to use Java's Collections or raw arrays. 2. Define the state variables: explicitly declare 'head', 'tail', and 'count' indices, explaining that 'count' simplifies the empty/full check compared to just comparing head and tail. 3. Draft the core logic: write the enqueue method first, focusing on updating the tail index using modulo arithmetic (tail = (tail + 1) % capacity) to handle wrap-around naturally. 4. Implement dequeue: mirror the enqueue logic for the head pointer, ensuring you only remove elements if the queue is not empty. 5. Optimize and verify: discuss time complexity (O(1)) and space complexity (O(N)), then walk through a trace example where the pointers wrap around the array boundary to prove correctness.

Key Points to Cover

  • Explicitly using a 'size' or 'count' variable to distinguish empty from full states
  • Correct application of modulo operator (%) for seamless wrap-around logic
  • Achieving strict O(1) time complexity for both enqueue and dequeue operations
  • Handling edge cases like initialization and array bounds without off-by-one errors
  • Demonstrating awareness of memory safety by nullifying removed references

Sample Answer

To implement a circular queue with a fixed-size array, I would start by defining three key instance variables: an array to store elements, an integer for the head index, and another for the tail index. Crucially, I would…

Common Mistakes to Avoid

  • Relying solely on head == tail to detect full/empty states without a count variable, leading to ambiguous conditions
  • Using simple addition (head + 1) instead of modulo arithmetic, causing IndexOutOfBoundsException when wrapping
  • Implementing linear shifting of elements on dequeue, resulting in O(N) complexity instead of O(1)
  • Failing to initialize the head and tail pointers correctly before starting operations

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 107 Microsoft questions