← All Exercises

Simulate a Ring Buffer Overwrite

basicjavascript~20min

Background

Ring buffers bound memory in constrained devices. When the producer outruns the consumer, overwrite counts are the only signal that data was lost.

Problem

Given a fixed capacity and a write sequence, return the buffer contents plus how many writes overwrote older data.

Input

JSON object: { capacity, writes: string[] }

Output

JSON object: { contents: string[], overwritten: number }

Sample tests

Sample 1

Input: {"capacity":3,"writes":["a","b","c","d"]}
Expected: {"contents":["b","c","d"],"overwritten":1}

Sample 2

Input: {"capacity":4,"writes":["a","b"]}
Expected: {"contents":["a","b"],"overwritten":0}

Your solution

Learning objective

Account for data loss in bounded firmware buffers.