Two-Type Ring Buffer
Problem statement
Implement a fixed-capacity FIFO ring buffer that holds values of two types: A is a signed 32-bit integer and B is a boolean. The capacity stays constant throughout the operation batch.
Process operations in order and return one result string for each operation:
pushA vandpushB b: append the typed value when space remains and returnTRUE. If the buffer is full, returnFALSEand leave it unchanged.pop: remove and return the oldest value, or returnEMPTYif empty.peek: return the oldest value without removing it, orEMPTYif empty.empty: returnTRUEexactly when the buffer is empty, otherwiseFALSE.full: returnTRUEexactly when the buffer is full, otherwiseFALSE.
Serialize returned values as A:v or B:true/B:false. Preserve insertion order and each value's type. Implement the final ring-buffer approach with constant work per operation.
Function
ringBuffer(capacity: int, operations: String[]) → String[]Examples
Example 1
capacity = 2operations = ["pushA 7","pushB true","full","pushA 9","peek","pop","pushA -2","pop","pop","empty"]return = ["TRUE","TRUE","TRUE","FALSE","A:7","A:7","TRUE","B:true","A:-2","TRUE"]The queue preserves insertion order and each value's type. A rejected push leaves the queue unchanged.
Example 2
capacity = 1operations = ["pop","peek","pushB false","pushA 3","pop","full"]return = ["EMPTY","EMPTY","TRUE","FALSE","B:false","FALSE"]The queue preserves insertion order and each value's type. A rejected push leaves the queue unchanged.
Constraints
1 <= capacity <= 10^51 <= operations.length <= 2 * 10^5- A values are between
-2^31and2^31 - 1, inclusive. - B values use exactly
trueorfalse. - Commands are valid, with one space between a push command and its canonical payload.
- Operations run sequentially in one thread.