Implement a Queue using a Circular Array

Data Structures
Medium
Microsoft
51K 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 also maintain a 'size' variable to track the current number of elements. This approach prevents ambiguity between an empty and full state, which often occurs if we rely solely on head and tail positions. For the enqueue operation, I will first check if the queue is full by comparing the size against the capacity. If it is full, I might throw an exception or return false, depending on the contract. Otherwise, I place the new element at the tail position and increment the tail index using modulo arithmetic: tail = (tail + 1) % capacity. This ensures that when the tail reaches the end of the array, it seamlessly wraps around to index zero. Similarly, for dequeue, I check if the queue is empty. If not, I retrieve the element at the head, set it to null to help garbage collection, and update the head index using the same modulo logic. By maintaining the size counter, both operations run in constant time O(1) regardless of the array size. This solution aligns well with Microsoft's focus on efficient resource utilization in backend services, ensuring no unnecessary memory allocation occurs during runtime while handling high-throughput data streams.

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

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 65 Microsoft questions