Put a ring on it.

Easy 1 10s 256 MB
BasicsData Structures Google

Implement RingBuffer, a fixed-capacity ring buffer for events. The buffer stores events in insertion order and automatically drops the oldest event when full.

Signature
RingBuffer(capacity)
  • Initializes an empty buffer with a maximum capacity of capacity.
  • capacity is guaranteed to be a positive integer.
Signature
push(event)
  • Event is a string type.
  • Inserts a new event into the buffer.
  • If the buffer is already full, the oldest event is dropped before inserting the new one.
  • This operation returns nothing.
    • Print null.
Signature
snapshot()
  • Returns all events currently stored in the buffer in oldest → newest order.
  • The returned events should be consecutive and space-separated when output.
  • Calling snapshot() must not modify the internal state of the buffer.
  • If the buffer is empty, print nothing (an empty line).
  • You may assume single-threaded usage.
  • The internal storage representation is up to you.
Example 1
Input
RingBuffer 2
push a
push b
push c
snapshot
Output
null
null
null
b c
Accepted 579/987
Acceptance 59%
Loading editor...
Input
RingBuffer 3
push 1
push 2
snapshot
Expected output
null
null
1 2