Ring Buffer

Medium ● Live C++ Rust Java Python

The Problem

Implement a high-performance circular buffer (ring buffer) for storing and retrieving market tick data. Support concurrent push/pop operations on a fixed-size buffer without dynamic memory allocation.

Scale: 5,000,000 operations (mixed push/pop). The ring buffer is a fundamental data structure in systems programming and high-frequency trading.

Evaluation Criteria

Correctness (70%)

All push/pop operations must maintain FIFO semantics. No data loss or corruption even at buffer capacity.

Performance (30%)

Wall-clock time on 5M mixed operations. Memory usage must be constant (no dynamic allocation).

Ring Buffer Specification

Data Structure

Fixed-size circular array with head and tail pointers. Buffer capacity: N elements.

Operations

Semantics

FIFO order: elements are popped in the same order they were pushed. Pushing to a full buffer overwrites oldest element.

Input Format

First line
N M where N = buffer capacity, M = number of operations.
Then M lines
one operation per line, from this set:

Output Format & Sample

Sample input (first 8 lines, full file linked below):

50 16
POP
PUSH 1 99.8 3658
PUSH 2 99.4 8936
PUSH 3 99.3 489
PUSH 4 99.0 9864
PUSH 5 98.7 8929
PUSH 6 98.7 4558
... (more)

Expected output for that sample (first 8 lines):

EMPTY
6 16
9 16
1 99.800000 3658
8 16
2 99.400000 8936
2 99.400000 8936
7 16
... (more)

↓ download full sample input  |  ↓ download expected output

Test your solution locally: ./your_solution < input.txt | diff - output.txt

Common Pitfalls

Performance Tier Thresholds (applied by the grader)

LanguageGoldSilverBronzeTier 4
C++<500ms<2000ms<8000mselse PASS
Rust<500ms<2000ms<8000mselse PASS
Java<1000ms<4000ms<15000mselse PASS
Python<4000ms<12000ms<50000mselse PASS

Implementation Notes

Memory Efficiency

Do NOT use dynamic allocation. Allocate buffer once at startup.

Wraparound

Use modulo arithmetic: next_index = (index + 1) % capacity

Thread Safety (Optional)

If using locks, minimize contention via fine-grained locking.

Submission Rules

Challenge Stats

Buffer capacity: 10,000
Operations: 5,000,000
Operation mix: 50% push, 50% pop
Gold Target: <400ms (C++)

Starter Templates

↓ skeleton.cpp ↓ skeleton.rs ↓ Skeleton.java ↓ skeleton.py

Submit Your Solution

You can submit multiple times. Best result per language counts.

Resources

Leaderboard →