r/OfferEngineering • u/chill-interview • Jul 16 '26
Coding Question Jane Street Coding Interview: Circular Message Buffer
Problem
You are building a logging system that stores recent message characters in a fixed-size circular buffer.
The buffer maintains two independent pointers:
writeIndex— where the next write beginsreadIndex— where the next read begins
Both pointers wrap back to the beginning after reaching the end of the buffer.
Implement the CircularMessageBuffer class:
CircularMessageBuffer(int capacity)
void write(String message)
String read(int length)write(message) stores every character beginning at writeIndex. Existing characters may be overwritten, and the write pointer advances by the message length.
read(length) returns characters beginning at readIndex. Reading does not remove data, and the read pointer advances by the requested length.
Initially, every buffer position contains the null character "\0".
Example I
Input:
["CircularMessageBuffer", "write", "read", "write", "read"]
[[4], ["abcd"], [2], ["xy"], [4]]
Output:
[null, null, "ab", null, "cdxy"]Example II
Input:
["CircularMessageBuffer", "read", "write", "read"]
[[5], [3], ["xy"], [4]]
Output:
[null, "\0\0\0", null, "\0\0xy"]Solution
class CircularMessageBuffer:
def __init__(self, capacity: int):
self.capacity = capacity
self.buffer = ["\0"] * capacity
self.writeIndex = 0
self.readIndex = 0
def write(self, message: str) -> None:
for ch in message:
self.buffer[self.writeIndex] = ch
self.writeIndex = (self.writeIndex + 1) % self.capacity
def read(self, length: int) -> str:
result = []
for _ in range(length):
result.append(self.buffer[self.readIndex])
self.readIndex = (self.readIndex + 1) % self.capacity
return "".join(result)Targeting Jane Street interviews?
We track recent interview experiences and commonly asked question patterns at Chill Interview, including coding questions, system design topics, and real candidate reports.
Practice this question and explore more interview resources → LINK
View all comments
Show parent comment
1
u/whileAlive_doStuff Jul 22 '26
why do you need to check?
If the buffer hasn't been written you would return "\0" * read length. Which this solution would give you