cswork / libraryOpen practice workspace

Interview questions · OpenAI

Parse and build binary data in Python

This question evaluates proficiency in binary data handling and serialization/deserialization in Python, including little-endian integer encoding, length-prefixed payloads, robust error handling for short reads and invalid lengths, and testability and performance considerations.

Using provided interfaces ByteReader(read(n), read_uint32_le, read_string) and ByteWriter(write(b), write_uint32_le, write_string), implement functions to pack and unpack messages for a simple binary protocol: message = {id:uint32 LE, payload_len:uint32 LE, payload:bytes}. Write parse_message(reader)->Message and build_message(writer, Message)->None with error handling for short reads, invalid lengths, and overflow. Avoid printing for debugging; design tests instead, and explain how you would verify correctness and performance.

Community answers

Nothing here yet.

Join in on CSWork →