Practice problem · Datadog · Medium

Batch Writer

A weather station on a glacier sends its readings home over a satellite uplink. 2 stages in Python written by you, with an assistant that only answers. Pro.

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 of send takes.
  • send(chunk) -> int: takes bytes from the front of chunk and returns how many it took, from 1 to len(chunk). It may take fewer than it was offered. It may raise; a call that raises has taken none of chunk.
  • 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) appends data (bytes, possibly empty) to the buffer. If the buffer then holds at least capacity bytes, it delivers the buffer.
  • Delivering calls send with the next min(max_chunk, bytes left) bytes, starting at the first byte not yet taken, and repeats until the buffer is empty. Never call send with an empty chunk, and make no other call to send: what each call returns decides the next one. Delivering because the buffer reached capacity never calls sync.
  • flush() delivers whatever is buffered (no send when nothing is), then calls sync() once, even when nothing was buffered.
  • close() flushes, then the writer is closed. write or flush on a closed writer raises ValueError; close on a closed writer does nothing (no send, no sync).

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 send raises, the exception leaves write or flush unchanged. The bytes not yet taken stay buffered, in order, ahead of anything written later (a write that raised has still added its data), and the next delivery starts with the first of them.
  • If delivery raises inside flush, sync is not called. If sync raises, the exception leaves flush; the bytes were already taken.
  • A buffer that a failed delivery left at or above capacity is delivered by the next write (whatever its data, empty included) or flush.
  • 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.py and the files in tests/
  • data is always bytes; capacity and max_chunk are 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.

  1. 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.

Start this problem → Sign in

More like it: every practice problem, Datadog's interviews, the AI coding interview guide.