FastPrepRing Buffer Operations
Problems/Ring Buffer Operations

Ring Buffer Operations

Amazon logoAmazon● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second-round loop of five interviewers for an Amazon senior firmware and BIOS engineer role, one topic was a ring buffer. The report names the topic and nothing else.

It does not say which operations the buffer has, what happens when it is full or empty, or how it was tested. Here, the buffer has a fixed capacity and four operations: write, read (removes the oldest value), peek and size. A write to a full buffer is rejected instead of overwriting the oldest value, reading or peeking an empty buffer gives -1, values are 0 to 10^9, and the limits and examples are ours. In the interview this would likely be a class; here the calls come as a list of operations.

Our version is about 70% the same as the reported question.

A ring buffer stores at most capacity integer values. It starts empty. Process the operations operations[0], operations[1], ... in order, and record one result for each:

  • "write": if the buffer holds fewer than capacity values, store values[i] as the newest value and record 1. If the buffer is full, the write is rejected: nothing changes and the result is 0.
  • "read": remove the oldest stored value and record it. If the buffer is empty, record -1.
  • "peek": record the oldest stored value without removing it, or -1 if the buffer is empty.
  • "size": record how many values are stored.

values[i] is only used by a write; for every other operation it is 0. Return the recorded results in order.

Function

runRingBuffer(capacity: int, operations: String[], values: int[]) → int[]

Examples

Example 1

capacity = 2operations = ["write","write","write","read","write","peek","size"]values = [5,7,9,0,9,0,0]return = [1,1,0,5,1,7,2]
  • write 5 and write 7 fill both slots, so each returns 1.
  • write 9 finds the buffer full, so it is rejected and returns 0.
  • read removes and returns the oldest value, 5.
  • Now there is room, so write 9 returns 1. The buffer holds 7 then 9.
  • peek returns the oldest value 7 without removing it, and size returns 2.

Example 2

capacity = 3operations = ["read","peek","size","write","read","read"]values = [0,0,0,42,0,0]return = [-1,-1,0,1,42,-1]

The buffer starts empty, so read and peek return -1 and size returns 0. write 42 returns 1, the next read returns 42, and the last read finds the buffer empty again and returns -1.

Example 3

capacity = 3operations = ["write","write","write","read","read","write","write","read","read","read"]values = [1,2,3,0,0,4,5,0,0,0]return = [1,1,1,1,2,1,1,3,4,5]

After writing 1, 2, 3 and reading 1 and 2, only 3 is stored. Writing 4 and 5 succeeds because the two freed slots can be reused. The three reads then return the values in the order they were written: 3, 4, 5.

Constraints

  • 1 <= capacity <= 1000
  • 1 <= operations.length <= 2000
  • values.length == operations.length
  • Each operations[i] is "write", "read", "peek" or "size".
  • 0 <= values[i] <= 10^9
  • values[i] == 0 whenever operations[i] is not "write".

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
  • Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
  • Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
  • Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
  • Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions
  • Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
  • Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
  • Amazon SDE II (L5) Interview Questions: 5 Experiences
See Amazon hiring insights
Saved
class Solution {
    public int[] runRingBuffer(int capacity, String[] operations, int[] values) {
        // Write your code here.
    }
}
Checking account…