Implement a single-producer multi-consumer ring buffer
CitadelSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of ring-buffer data structures, circular indexing, fixed-capacity storage, and object-oriented design for a single-producer, multi-consumer implementation in C++.
Implement a fixed-capacity ring buffer in C++ for a setting with one producer and multiple consumers.
Design a class that stores integers in a preallocated circular array and supports the following operations:
RingBuffer(int capacity): initialize the buffer with a fixed capacity.bool push(int value): insert a value at the tail. Returnfalseif the buffer is full.bool pop(int& value): remove the oldest value from the head and write it intovalue. Returnfalseif the buffer is empty.bool empty() constbool full() constint size() const
Requirements:
- All operations should run in O(1) time.
- Use circular indexing to reuse freed space.
- Do not resize the underlying storage.
- Focus on the object-oriented design and correct ring-buffer behavior.
- You may assume synchronization is handled externally; the main goal is to implement the ring-buffer logic correctly for a single-producer, multi-consumer use case.
After coding, briefly explain how you would adapt the design if true thread safety were required.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.