Parse and Format Arbitrarily Nested Tasks from CSV
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
Parse CSV records describing tasks and recursively nested subtasks, then format the resulting forest with distinct connectors for final and non-final children.
## Function Contract
Implement `format_task_csv(lines)` and return a list of output lines.
## Rules
- A root record has `timestamp,task,task_id,task_name`.
- A child record has `timestamp,subtask,parent_id,task_id,task_name`; any child may itself be a parent.
- Input is valid CSV, so quoted task names may contain commas and escaped quotes.
- Use the same tree-prefix rules as a conventional file tree: `├─ ` for a non-final child, `└─ ` for a final child, and carry either `│ ` or three spaces through ancestor levels.
- Format each node as `task_id + " " + task_name`, preserve root and sibling input order, and ignore timestamps for ordering.
## Constraints
- `0 <= len(lines) <= 200000`.
- Every parent record appears before its children.
- Task IDs are unique and task names contain no newline characters.
## Examples
```text
lines = [
"01/01/2025,task,T1,cook dinner",
"01/01/2025,subtask,T1,T2,buy groceries",
"01/01/2025,subtask,T1,T3,prepare meal"
]
output = [
"T1 cook dinner",
"├─ T2 buy groceries",
"└─ T3 prepare meal"
]
```
Quick Answer: Parse valid CSV records into an arbitrarily nested task forest and render deterministic tree lines while preserving quoted names, root order, sibling order, and correct ancestor connectors.
Parse valid CSV records that describe a forest of tasks and recursively nested subtasks, then return the formatted tree lines. A root record has fields timestamp, task, task_id, task_name. A child record has fields timestamp, subtask, parent_id, task_id, task_name, and any child may itself be a parent. Quoted task names may contain commas and escaped quotes. Format each node as task_id + one space + task_name. Render roots without a connector; use ├─ for a non-final child and └─ for a final child, carrying │ followed by two spaces or three spaces through ancestor levels. Preserve root and sibling input order and ignore timestamps for ordering.
Constraints
- 0 <= len(lines) <= 200000.
- Every line is valid CSV, every parent appears before its children, and task IDs are unique.
- Task names contain no newline characters.
- Root and sibling order follow input order; timestamps do not affect ordering.
Examples
Input: (['01/01/2025,task,T1,cook dinner', '01/01/2025,subtask,T1,T2,buy groceries', '01/01/2025,subtask,T1,T3,prepare meal'],)
Expected Output: ['T1 cook dinner', '├─ T2 buy groceries', '└─ T3 prepare meal']
Explanation: Two root children receive non-final and final connectors in input order.
Input: ([],)
Expected Output: []
Explanation: No records produce no formatted lines.
Hints
- A comma separates fields only while the CSV scanner is outside a quoted field.
- Carry ancestor finality in the prefix while traversing each root's children in input order.