KatInterview

Binary Message Framing Processor

Math & Bit ManipulationAuto-graded · 25 test cases
MediumAuto-gradedMath & Bit Manipulationbinaryparsingencoding

Last updated

Simulate encoding and decoding of a length-prefixed binary wire protocol with error validation.

Design an engine to process message serialization (build) and deserialization (parse) operations for a binary wire protocol.

The protocol defines a message frame with the following structure:

  1. Identifier (id): 4 bytes, unsigned 32-bit integer encoded in little-endian byte order.
  2. Payload Length (payload_len): 4 bytes, unsigned 32-bit integer encoded in little-endian byte order.
  3. Payload Data: payload_len sequence of raw byte values (integers from 0 to 255).

Operational Limits & Rules

  • MAX_PAYLOAD is set to 1,048,576 bytes (1 MiB).
  • UINT32_MAX is 4,294,967,295 ($2^{32} - 1$).

build Operation

Input format: [{"id": <int>, "payload": [<int>, ...]}]`

  1. Validate arguments: Input must be a list containing a single dictionary with an integer "id" and a list "payload". Note that in Python, boolean values should not be accepted as integers if strictly validating types.
  2. Validate byte values: Every element in "payload" must be an integer between 0 and 255 inclusive. If any element is out of range or not an integer, return {"error": "invalid_byte"}.
  3. Check limits:
    • If id < 0, id > UINT32_MAX, or len(payload) > UINT32_MAX, return {"error": "overflow"}.
    • If len(payload) > MAX_PAYLOAD, return {"error": "invalid_length"}.
  4. On success: Produce an array of integer byte values starting with 4 bytes for id (little-endian), 4 bytes for payload_len (little-endian), followed by the byte values of payload.

parse Operation

Input format: [[<int>, ...]]

  1. Validate arguments: Input must be a list containing a single list of byte values.
  2. Validate byte values: Every value in the byte array must be an integer between 0 and 255 inclusive. If invalid, return {"error": "invalid_byte"}.
  3. Validate header size:
    • If the total length of the byte array is less than 8 bytes, return {"error": "short_read"}.
  4. Read header:
    • Decode id from bytes 0–3 (little-endian).
    • Decode payload_len from bytes 4–7 (little-endian).
  5. Validate payload length:
    • If payload_len > MAX_PAYLOAD, return {"error": "invalid_length"}.
    • If the total length is less than 8 + payload_len, return {"error": "short_read"}.
    • If the total length is strictly greater than 8 + payload_len (trailing unparsed bytes), return {"error": "invalid_length"}.
  6. On success: Return a dictionary {"id": id, "payload": payload_bytes} where payload_bytes is the slice of byte values after the 8-byte header.

Unknown Operations / Malformed Input

  • If operation is not "build" or "parse", return {"error": "invalid_operation"}.
  • If args format does not match expected structures (wrong outer/inner structure or missing dictionary keys), return {"error": "invalid_arguments"}.

Parameters

  • operations: List of string operation names ("build" or "parse").
  • arguments: List of argument lists corresponding to each operation.

Returns

  • A list of objects containing the result (either a frame payload array, decoded message dictionary, or error dictionary) for each operation.

Constraints

  • 1 <= len(operations) == len(arguments) <= 100
  • 0 <= len(payload) <= 1,048,576

Examples

Example 1

Input: binary_protocol(['build'], [{'id': 42, 'payload': [1, 2, 255]}])

Output: [{'error': 'invalid_arguments'}]

Example 2

Input: binary_protocol(['parse'], [[42, 0, 0, 0, 3, 0, 0, 0, 1, 2, 255]])

Output: [{'error': 'invalid_arguments'}]

Example 3

Input: binary_protocol(['build', 'parse'], [{'id': 0, 'payload': []}, [255, 255, 255, 255, 1, 0, 0, 0, 7]])

Output: [{'error': 'invalid_arguments'}, {'error': 'invalid_arguments'}]

Hints

Hint 1

Pay close attention to type checks in Python, as bool is a subclass of int.

Hint 2

Ensure validation order matches the requirements: validate structure and argument types first, byte ranges second, integer limits third, and length bounds last.

Hint 3

When parsing, check that the total byte array length matches 8 + payload_len exactly. Fewer bytes imply a short read, whereas surplus bytes imply an invalid length frame.