The Line Splitter
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L4
Problem
Each string in lines is one line of a CSV export: the first holds the column headers, and every comma after it is a column boundary, quote characters included. Return one record per data line mapping each header to that line's value, with the records ordered by their first column's value.
Comma-separated truths, one at a time.
Example
Input:
lines = ["name,age,city","Carol,41,Denver","Alice,30,Boston","Bob,25,NYC"]
Output:
[{"name":"Alice","age":"30","city":"Boston"},{"name":"Bob","age":"25","city":"NYC"},{"name":"Carol","age":"41","city":"Denver"}]Worked solution and explanation
The trap is the library
This is a test of whether you will do exactly what the contract says instead of what feels professional. The contract is primitive on purpose: every comma is a boundary, quotes are just characters. The reflex is csv.DictReader, because hand-splitting CSV feels like a junior move. But DictReader speaks RFC 4180: hand it Widget,"special" and it quietly eats the quotes and returns 'special'. The graded answer keeps the literal '"special"', so the robust-looking tool is the wrong one here.
The second thing it checks is that you honor the order the prompt names even though the file does not. The example lists Carol before Alice and expects Alice first: the first column is the row's identifier and the output follows it, not the file.
Step 1: Split the header once
lines[0].split(',') gives the column names. Grab headers[0] as the identifier column now, so the ordering step later has nothing to rediscover.
Step 2: Pair each data line with the headers
Walk lines[1:] so the header never becomes a record. zip(headers, values) pairs by position, and because split interprets nothing, an empty field stays '', 'New York' keeps its space, and '07' keeps its zero.
Step 3: Order by the identifier column
sorted(records, key=record_id) orders by the first column's text. A header-only file never enters the loop, so the empty list falls out with no special case.
def parse_csv_rows(lines):
headers = lines[0].split(',')
id_column = headers[0]
records = []
for line in lines[1:]:
values = line.split(',')
columns = list(zip(headers, values))
record = {header: value for header, value in columns}
records.append(record)
def record_id(record):
return record[id_column]
return sorted(records, key=record_id)- csv.DictReader
Widget,"special"becomes 'special': the quotes are treated as field delimiters and stripped.- Plain split
line.split(',')returns '"special"' untouched, which is what the export actually contains and what grades.
Common follow-up questions
- Now fields can contain commas inside quotes. What changes? (Whether you know when the `csv` module becomes the right tool.)
- How would you handle a line with fewer fields than `headers`? (Padding with '' versus rejecting the line.)
- The export is ten gigabytes. Can you still return it ordered? (Streaming versus the memory cost of a global sort.)