FastPrepTwo-Type Ring Buffer

Two-Type Ring Buffer

IMC Trading logoIMC Trading● MediumNEW GRADOA
Learn

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 v and pushB b: append the typed value when space remains and return TRUE. If the buffer is full, return FALSE and leave it unchanged.
  • pop: remove and return the oldest value, or return EMPTY if empty.
  • peek: return the oldest value without removing it, or EMPTY if empty.
  • empty: return TRUE exactly when the buffer is empty, otherwise FALSE.
  • full: return TRUE exactly when the buffer is full, otherwise FALSE.

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^5
  • 1 <= operations.length <= 2 * 10^5
  • A values are between -2^31 and 2^31 - 1, inclusive.
  • B values use exactly true or false.
  • Commands are valid, with one space between a push command and its canonical payload.
  • Operations run sequentially in one thread.

More IMC Trading problems

See IMC Trading hiring insights
public String[] ringBuffer(int capacity, String[] operations) {
    // Write your solution here.
}
capacity2
operations["pushA 7","pushB true","full","pushA 9","peek","pop","pushA -2","pop","pop","empty"]
expected["TRUE", "TRUE", "TRUE", "FALSE", "A:7", "A:7", "TRUE", "B:true", "A:-2", "TRUE"]
Checking account…