Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Circular Buffer for Embedded

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

Your question is Circular Buffer for Embedded. 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

An Amazon Echo device uses a fixed-size memory region to buffer events before they are processed. Implement a circular buffer with overwrite-oldest behavior: when a push occurs on a full buffer, discard the oldest element and retain the new one.

Implement process_circular_buffer(capacity, operations). Each operation is an array whose first element is a command:

  • ["push", value]: insert one integer.
  • ["push_many", values]: insert all integers in order.
  • ["pop"]: remove and return the oldest value, or None if empty.
  • ["peek"]: return the oldest value without removing it, or None if empty.
  • ["snapshot"]: return all values from oldest to newest.
  • ["size"]: return the current number of stored values.
  • ["is_full"]: return whether the buffer is full.

Return an array containing the result of every operation that produces a value: pop, peek, snapshot, size, and is_full. Push operations produce no result.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= len(operations) <= 10^5
  • The total number of values in all push_many operations is at most 10^6
  • Pushed values are integers in the range -10^9 through 10^9
  • Every operation uses a valid command format

Function Signature

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