← Questions

Assemble objects from chunks of an async stream

0
MicrosoftCodingLast asked Feb 2026

A coding interview question asked in Microsoft's coding round, last asked Feb 2026. It tests streams, parsing and async.

Reads from an asynchronous byte stream return arbitrary chunks: one object can be split across several reads, and one read can hold several objects or parts of them. Objects are framed as a 4-byte big-endian length followed by that many bytes of payload. Write code that buffers the chunks and yields each complete payload as soon as it is available.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • stream.read(n: int) is awaitable and returns between 1 and n bytes, or b"" at end of stream; it may raise on an I/O error.
  • async def read_records(stream) -> AsyncIterator[bytes] — yields each payload in order. A clean end of stream between records ends the iteration; an end of stream in the middle of a record raises an error.

Example

chunks = [b"\x00\x00", b"\x00\x03ab", b"c\x00\x00\x00\x00\x00\x00\x00\x02h", b"i"]
read_records(stream over chunks)  ->  b"abc", b"", b"hi"
    # the length of "abc" is split across reads; the third chunk ends "abc",
    # holds an empty record, and starts "hi"

chunks = [b"\x00\x00\x00\x05abc"]
read_records(stream over chunks)  ->  raises: stream ended inside a record

Constraints

  • Records can be up to 16 MB; the stream can carry millions of them.
  • Avoid copying the whole buffer on every read (no quadratic behaviour).

Clarifying questions

  • What is the framing: length prefix (how many bytes, which byte order), or a delimiter?
  • Is there a maximum record size, and what should happen when a length exceeds it?
  • Should a truncated final record be an error or be dropped?
  • Is it one consumer, or can several tasks read concurrently?

What a strong answer covers

  • A buffer plus a small state machine: waiting for the 4-byte header, then for the payload of known length.
  • Yielding every complete record already in the buffer before awaiting another read, so a chunk with several records emits them all.
  • Efficient buffering (a bytearray with an offset, or a list of chunks) so consuming the front is not O(buffer) each time.
  • End of stream and errors: clean EOF at a boundary, EOF mid-header or mid-payload, I/O exceptions propagated, a cap on declared length to stop a bad header from allocating gigabytes.
  • Tests that split the same byte string at every possible boundary, plus zero-length records.

Follow-ups

  • The framing is newline-delimited JSON instead.
  • Add backpressure: stop reading when the consumer falls behind.
  • Support cancellation mid-read without losing buffered data.
  • Write the same in C# with Stream.ReadAsync and Memory<byte>, or with pipelines.

Where it's asked

  • Microsoft · Coding · Last asked Feb 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent

What it tests

streams · parsing · async

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding