# The Log Decoder

> Every line holds a secret.

Canonical URL: <https://datadriven.io/problems/the_log_decoder>

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a list of log lines, each a space-separated sequence of key=value tokens, return a list of dicts. Each dict has the same key=value pairs converted to dict entries; values remain strings.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string parsing into structured data**, a daily task in data engineering. Interviewers check whether candidates can split on multiple delimiters and build dictionaries from key-value pairs without overengineering the solution.

---

### Break down the requirements

#### Step 1: Split each line on spaces

Each space-separated token is a `key=value` pair.

#### Step 2: Split each token on the equals sign

The part before `=` is the key, the part after is the value. Both remain as strings.

#### Step 3: Build a dict per line

Collect all key-value pairs from one line into a dictionary, then append it to the result list.

---

### The solution

**Two-level split: spaces then equals**

```python
def parse_logs(lines):
    result = []
    for line in lines:
        entry = {}
        tokens = line.split(' ')
        for token in tokens:
            parts = token.split('=', 1)
            entry[parts[0]] = parts[1]
        result.append(entry)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n * k) where n is the number of lines and k is the average number of tokens per line.
> 
> **Space:** O(n * k) for the output list of dictionaries.

> **Interviewers Watch For**
>
> Using `split('=', 1)` with a maxsplit of 1. If a value itself contains `=`, splitting without a limit would break. This is a signal of production-level thinking.

> **Common Pitfall**
>
> Not handling values that contain spaces. The problem guarantees space-separated key=value pairs, but mentioning this assumption shows awareness of real-world log parsing challenges.

---

## Common follow-up questions

- What if values could contain spaces (e.g., msg="request timed out")? _(Tests handling of quoted values in log parsing.)_
- How would you handle malformed lines with missing equals signs? _(Tests defensive parsing with try/except or validation.)_
- What if the log volume is 10GB per day? _(Tests streaming vs batch processing awareness.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_log_decoder)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.