Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Fixed Size Ring Buffer

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Fixed Size Ring Buffer. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Deliveroo services may need a bounded in-memory queue for short-lived order events. Implement a fixed-capacity ring buffer that processes commands in constant time while preserving FIFO order.

Write process_ring_buffer(capacity, operations). The buffer initially contains no items. Each operation is represented as a list:

  1. ["enqueue", value]: add value to the back. Return true if successful, or false if the buffer is full.
  2. ["dequeue"]: remove and return the oldest value, or return null if empty.
  3. ["peek"]: return the oldest value without removing it, or null if empty.
  4. ["size"]: return the current number of stored values.

Return an array containing one result for every operation. The implementation must use a fixed-size array internally, reuse positions after wraparound, and must not shift elements during removal.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= operations.length <= 10^5
  • Enqueue values are integers or strings
  • Operation names and formats are valid
  • The total input size is at most 10^5 operations

Function Signature

def process_ring_buffer(capacity, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output