Ring Buffer Operations
Updated
Problem statement
Implement a fixed-capacity ring buffer and process a batch of operations in order. The buffer starts empty.
For each index i, execute operations[i] and append one integer to the result:
push: if the buffer is not full, insertvalues[i]at the back and append1. If the buffer is full, leave it unchanged and append0.pop: if the buffer is not empty, remove and append the oldest value. If it is empty, append-1.front: if the buffer is not empty, append the oldest value without removing it. If it is empty, append-1.size: append the current number of values in the buffer.
When either end reaches the physical end of the backing array, it must wrap around to the beginning.
Function
processRingBuffer(capacity: int, operations: String[], values: int[]) → int[]Examples
Example 1
capacity = 3operations = ["push","push","front","pop","size"]values = [10,20,0,0,0]return = [1,1,10,10,1]The two pushes succeed. The front value is 10; popping removes and returns it, leaving one value in the buffer.
Example 2
capacity = 2operations = ["push","push","push","pop","push","front","size"]values = [5,6,7,0,8,0,0]return = [1,1,0,5,1,6,2]The third push fails because the capacity is 2. After popping 5, pushing 8 reuses the freed circular slot. The front remains 6.
Example 3
capacity = 1operations = ["pop","front","size","push","push","pop","pop","size"]values = [0,0,0,42,99,0,0,0]return = [-1,-1,0,1,0,42,-1,0]Empty reads return -1. The first push succeeds, the next push fails while full, and popping 42 makes the capacity-one buffer empty again.
Constraints
1 <= capacity <= 10^51 <= operations.length = values.length <= 10^5- Every operation is
push,pop,front, orsize. 0 <= values[i] <= 10^9values[i]is used only whenoperations[i]ispush.
More Applied Intuition interview questions
- KV Store with Nested TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Parse Nested Message TypesPHONE SCREEN · Seen Nov 2026
- Resolve Variable Equations with Dependency ErrorsPHONE SCREEN · Seen Nov 2026
- Locate Vehicles with Square QueriesPHONE SCREEN · Seen Nov 2026
- File-Backed Dense Game of LifePHONE SCREEN · Seen Oct 2026
- Merge Collinear Point SegmentsPHONE SCREEN · Seen Oct 2026
- Compilation Order with Topological SortPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Merge Overlapping Closed IntervalsPHONE SCREEN · Seen Sep 2026
- Out-of-Order TLV File ReassemblyPHONE SCREEN · Seen Aug 2026
- Hash Map with Separate ChainingONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- VIP and Regular Service SchedulerIntuit · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREEN
- Stateful Paginated Fetch NLyft · PHONE SCREEN
- Music Player Operation SequenceAtlassian · PHONE SCREEN
- Recent Event Stream QueriesSnowflake · PHONE SCREEN
- Machine Message RouterOpenAI · PHONE SCREEN
- Article Vote TrackerRippling · PHONE SCREEN · ONSITE INTERVIEW
- Count Hits in the Last Five MinutesDecagon · PHONE SCREEN
- Completions API Rate LimiterCursor · PHONE SCREEN