Parse semicolon-delimited telemetry key-value pairs into a typed dictionary

Quick Overview

Parse a semicolon-delimited telemetry string of key=value pairs into a dictionary with typed values. The task tests careful string handling: trimming, splitting at the first equals sign, skipping malformed segments, keeping the last duplicate key, and inferring integer, float, boolean and null values.

Parse semicolon-delimited telemetry key-value pairs into a typed dictionary

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A telemetry feed delivers readings as raw strings of `key=value` pairs separated by semicolons (`;`), and some of those strings are malformed. Write a parser that converts one telemetry string into a dictionary (a JSON object) with typed values. The parser trims whitespace around keys and values, keeps the last occurrence when a key repeats, maps an empty value such as `key=` to `None`, skips segments that contain no `=`, and splits each segment only at its first `=`, so a value may contain more `=` characters. Integer values become `int`, float values become `float`, `true` and `false` in any letter case become booleans, and every other value stays a trimmed string. An empty or whitespace-only input returns an empty dictionary. ### Function Signature ```python def parse_telemetry(telemetry: str) -> dict: ... ``` ### Rules - Split the input on `;`. Each piece is a segment. A segment with no `=` is skipped, and that includes empty and whitespace-only segments. - Split a segment at its first `=`. The text before it is the key and the text after it is the raw value. Trim leading and trailing whitespace from both, as Python's `str.strip()` does. - A segment whose key is empty after trimming (for example `=5`) is skipped. - Type the trimmed value by checking these cases in order: 1. An empty string becomes `None`. 2. `true` or `false` in any letter case becomes `True` or `False`. 3. An optional `+` or `-` followed by one or more digits `0-9`, and nothing else, becomes an `int`. Leading zeros are allowed, so `007` becomes `7`. 4. An optional sign, then either digits with an optional fractional part (`36.6`, `3.`) or a fractional part alone (`.5`), then an optional exponent (`e` or `E`, an optional sign, one or more digits) becomes a `float`. For example `1e3` becomes `1000.0`. 5. Anything else stays a string. That includes `inf`, `nan`, `1_000`, `0x1F` and `1.2.3`. - When a key appears more than once, the value from its last occurrence is kept. Key order in the returned dictionary does not matter. ### Constraints - `0 <= len(telemetry) <= 10^4` - Keys and values may have leading or trailing whitespace. ### Examples ```text Input: telemetry = "sensor_id=42; temperature=36.6; active=true; label=main unit" Output: {"sensor_id": 42, "temperature": 36.6, "active": True, "label": "main unit"} ``` There are four valid pairs. `42` is an integer, `36.6` is a float, `true` is a boolean, and `main unit` matches no other type, so it stays a string with its inner space. ```text Input: telemetry = "key1=hello; key1=world; empty=; novalue; key2=3.14; bool=FALSE" Output: {"key1": "world", "empty": None, "key2": 3.14, "bool": False} ``` `key1` appears twice and keeps its last value, `world`. `empty=` has an empty value, so it maps to `None`, and `novalue` has no `=`, so it is skipped. `FALSE` is a boolean because the match ignores case. ```text Input: telemetry = " query = mode=fast ; =orphan ;count= -7;ratio=1e3;; flag = True " Output: {"query": "mode=fast", "count": -7, "ratio": 1000.0, "flag": True} ``` `query` is split only at its first `=`, so its value is `mode=fast`. `=orphan` has an empty key and the empty segment between `;;` has no `=`, so both are skipped. `-7` is an integer, `1e3` is a float in exponent form, and `True` is a boolean.

Overview: Parse a semicolon-delimited telemetry string of key=value pairs into a dictionary with typed values. The task tests careful string handling: trimming, splitting at the first equals sign, skipping malformed segments, keeping the last duplicate key, and inferring integer, float, boolean and null values.

Spacex logo
Spacex
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A telemetry feed delivers readings as raw strings of key=value pairs separated by semicolons (;), and some of those strings are malformed. Write a parser that converts one telemetry string into a dictionary (a JSON object) with typed values.

The parser trims whitespace around keys and values, keeps the last occurrence when a key repeats, maps an empty value such as key= to None, skips segments that contain no =, and splits each segment only at its first =, so a value may contain more = characters. Integer values become int, float values become float, true and false in any letter case become booleans, and every other value stays a trimmed string. An empty or whitespace-only input returns an empty dictionary.

Function Signature

def parse_telemetry(telemetry: str) -> dict:
    ...

Rules

  • Split the input on ; . Each piece is a segment. A segment with no = is skipped, and that includes empty and whitespace-only segments.
  • Split a segment at its first = . The text before it is the key and the text after it is the raw value. Trim leading and trailing whitespace from both, as Python's str.strip() does.
  • A segment whose key is empty after trimming (for example =5 ) is skipped.
  • Type the trimmed value by checking these cases in order:
    1. An empty string becomes None .
    2. true or false in any letter case becomes True or False .
    3. An optional + or - followed by one or more digits 0-9 , and nothing else, becomes an int . Leading zeros are allowed, so 007 becomes 7 .
    4. An optional sign, then either digits with an optional fractional part ( 36.6 , 3. ) or a fractional part alone ( .5 ), then an optional exponent ( e or E , an optional sign, one or more digits) becomes a float . For example 1e3 becomes 1000.0 .
    5. Anything else stays a string. That includes inf , nan , 1_000 , 0x1F and 1.2.3 .
  • When a key appears more than once, the value from its last occurrence is kept. Key order in the returned dictionary does not matter.

Constraints

  • 0 <= len(telemetry) <= 10^4
  • Keys and values may have leading or trailing whitespace.

Examples

Input: telemetry = "sensor_id=42; temperature=36.6; active=true; label=main unit"
Output: {"sensor_id": 42, "temperature": 36.6, "active": True, "label": "main unit"}

There are four valid pairs. 42 is an integer, 36.6 is a float, true is a boolean, and main unit matches no other type, so it stays a string with its inner space.

Input: telemetry = "key1=hello; key1=world; empty=; novalue; key2=3.14; bool=FALSE"
Output: {"key1": "world", "empty": None, "key2": 3.14, "bool": False}

key1 appears twice and keeps its last value, world. empty= has an empty value, so it maps to None, and novalue has no =, so it is skipped. FALSE is a boolean because the match ignores case.

Input: telemetry = " query = mode=fast ; =orphan ;count= -7;ratio=1e3;; flag = True "
Output: {"query": "mode=fast", "count": -7, "ratio": 1000.0, "flag": True}

query is split only at its first =, so its value is mode=fast. =orphan has an empty key and the empty segment between ;; has no =, so both are skipped. -7 is an integer, 1e3 is a float in exponent form, and True is a boolean.