Stage 1 of 2: Batch writes
Background. A weather station on a glacier sends its readings home over a satellite uplink. Every call to the uplink is a slow round trip, so the station collects small writes in memory and passes them on in batches, calling the uplink far less often.
Implement BatchWriter in batch_writer.py: the constructor BatchWriter(uplink, capacity) and the methods write(data), flush() and close().
The uplink
uplink.py (read-only) describes the interface, Uplink, and gives MemoryUplink, an uplink in memory to try your writer with. An uplink has:
max_chunk: an int of at least 1, the most bytes one call ofsendtakes.send(chunk) -> int: takes bytes from the front ofchunkand returns how many it took, from 1 tolen(chunk). It may take fewer than it was offered. It may raise; a call that raises has taken none ofchunk.sync() -> None: makes every byte taken so far durable. It may raise.
The writer
capacity is an int of at least 1, and the buffer starts empty.
write(data)appendsdata(bytes, possibly empty) to the buffer. If the buffer then holds at leastcapacitybytes, it delivers the buffer.- Delivering calls
sendwith the nextmin(max_chunk, bytes left)bytes, starting at the first byte not yet taken, and repeats until the buffer is empty. Never callsendwith an empty chunk, and make no other call tosend: what each call returns decides the next one. Delivering because the buffer reachedcapacitynever callssync. flush()delivers whatever is buffered (nosendwhen nothing is), then callssync()once, even when nothing was buffered.close()flushes, then the writer is closed.writeorflushon a closed writer raisesValueError;closeon a closed writer does nothing (nosend, nosync).
send may be given bytes, a bytearray or a memoryview slice: the tests' uplinks copy each chunk with bytes(chunk) as it arrives.
When the uplink fails
- If
sendraises, the exception leaveswriteorflushunchanged. The bytes not yet taken stay buffered, in order, ahead of anything written later (awritethat raised has still added itsdata), and the next delivery starts with the first of them. - If delivery raises inside
flush,syncis not called. Ifsyncraises, the exception leavesflush; the bytes were already taken. - A buffer that a failed delivery left at or above
capacityis delivered by the nextwrite(whatever itsdata, empty included) orflush. - If the flush inside
close()raises,close()raises that same exception and the writer stays open.
Example
from batch_writer import BatchWriter
from uplink import MemoryUplink
link = MemoryUplink(max_chunk=5, takes=[None, None, None, 2])
writer = BatchWriter(link, capacity=8)
writer.write(b"melt7;") # 6 bytes buffered: no call
writer.write(b"ok") # 8 bytes: send(b"melt7") -> 5, send(b";ok") -> 3; no sync
writer.write(b"temp=-4.5;wind=NE") # 17 bytes: send(b"temp=") -> 5, send(b"-4.5;") -> 2,
# send(b".5;wi") -> 5, send(b"nd=NE") -> 5
writer.flush() # nothing buffered: sync() only
writer.close() # flushes again: sync() only; the writer is now closed
writer.close() # no call
The fourth send takes only 2 of the 5 bytes it is offered (takes says so), so the next chunk starts at .5;wi, the first bytes not taken. In the end link.received is b"melt7;oktemp=-4.5;wind=NE".
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
batch_writer.py - Read-only:
uplink.pyand the files intests/ datais alwaysbytes;capacityandmax_chunkare ints of at least 1
What comes after stage 1
The next stage opens once the tests for the one before it pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Many threads. Several threads write and flush through one writer at once, and every record must still arrive whole.
The files you get
- Yours to edit: batch_writer.py.
- Read-only: uplink.py, tests/__init__.py, tests/fakes.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
Datadog runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Batch Writer free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
Datadog. It is modelled on Datadog's coding round, which runs without AI. Based on candidates' reports of Datadog interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
4 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, Datadog's interviews, the AI coding interview guide.