Ring Buffer Operations
Updated
Problem statement
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 thancapacityvalues, storevalues[i]as the newest value and record1. If the buffer is full, the write is rejected: nothing changes and the result is0."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-1if 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 5andwrite 7fill both slots, so each returns1.write 9finds the buffer full, so it is rejected and returns0.readremoves and returns the oldest value,5.- Now there is room, so
write 9returns1. The buffer holds7then9. peekreturns the oldest value7without removing it, andsizereturns2.
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 <= 10001 <= operations.length <= 2000values.length == operations.length- Each
operations[i]is"write","read","peek"or"size". 0 <= values[i] <= 10^9values[i] == 0wheneveroperations[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