# Where the Lines Break

> Every record hides its fields behind noise. Pull the real ones loose.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Access-log lines arrive with ragged whitespace: fields run together under uneven spacing and stray leading or trailing gaps. Break each `line` into its whitespace-separated fields, keeping only the real tokens, and hand back an empty list when a line holds nothing but whitespace.

## Worked solution and explanation

### What this really is

Underneath the log-parsing costume, this is a one-liner whose entire difficulty is one fork: `.split()` versus `.split(' ')`. The bare call collapses every run of whitespace and silently drops the empty gaps, so padded and ragged lines come out clean. Pass a literal space and Python preserves every empty string sitting between your double spaces. Reach for `.split(' ')` on a line like `'  POST   /login  '` and you hand back a list studded with `''` tokens that no downstream parser asked for, and it will not split a tab- or newline-delimited line at all.

---

### How to get there

#### Step 1: Split on whitespace runs

Call `.split()` with no arguments on `line`. The argument-less form treats any maximal run of whitespace (spaces, tabs, newlines) as a single delimiter, so inconsistent padding stops mattering.

#### Step 2: Let the default drop the empties

There is no second step. The no-argument split also discards leading, trailing, and interior empty fields, so the non-empty tokens are exactly what you return. An all-whitespace line falls out as an empty list for free.

---

### The solution

**Default split with automatic whitespace handling**

```python
def tokenize(line: str) -> list:
    return line.split()
```

*No arguments means runs of whitespace collapse and empty tokens vanish.*

> **Time and space**
>
> **Time:** O(n) over the length of the line. **Space:** O(n) for the token list. A single linear scan, nothing redundant.

> **Interviewers watch for**
>
> Whether you know the difference between `.split()` and `.split(' ')`. The first collapses whitespace and drops empties; the second keeps an empty string for every extra space. Naming that distinction unprompted is the tell of someone who has been burned by it before.

> **Common pitfall**
>
> Writing `.split(' ')` and then filtering empties with a comprehension. It produces the right answer on space-only input but reimplements, verbosely, what the bare `.split()` already does, and it quietly breaks the moment a tab or newline shows up in the line.

**line.split(' ')**

On '  POST   /login  ' returns ['', '', 'POST', '', '', '/login', '', '']. You now own the empty-token cleanup, and tabs/newlines are not separators.

**line.split()**

On the same input returns ['POST', '/login']. Whitespace runs collapse, edges trim, empties drop, and tabs and newlines are handled too.

---

## Common follow-up questions

- What if fields were separated by a single '|' instead of whitespace, and empty fields were meaningful? _(Tests .split('|') and the need to filter or keep empty tokens deliberately.)_
- What changes if a line mixes tabs and newlines into the spacing? _(Confirms .split() already treats tabs and newlines as separators.)_
- How would you tokenize while keeping the delimiters in the output? _(Tests re.split with a capturing group to retain the separators.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/where_the_lines_break)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.