FunctionRacerProblemsRace

Ring Buffer That Overwrites

EasyClassesPython, JavaScript, TypeScript, Java, C++

Implement a class RingBuffer, a buffer of integers with room for capacity values that overwrites its oldest value once it is full. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

RingBuffer(3)
write(1) returns -1
write(2) returns -1
write(3) returns -1
write(4) returns 1
to_list() returns [2,3,4]
read() returns 2
count_free() returns 1
write(5) returns -1
to_list() returns [3,4,5]

The buffer fills with 1 2 3. Writing 4 throws away the oldest value 1. Reading takes 2, which leaves one free slot, so writing 5 overwrites nothing.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems