Problem · Parsing

Render a Task Tree from CSV Rows

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

You are given valid CSV rows describing tasks and nested subtasks. Convert the rows into an ordered forest and return its text rendering.

Each row has one of these two shapes:

  • A root task: timestamp,task,taskId,taskName.
  • A subtask: timestamp,subtask,parentId,taskId,taskName.

The timestamp is metadata and does not appear in the result. Task IDs are unique, every parent row appears before its children, and roots and siblings retain their input order.

Render the forest in preorder. A root line is taskId taskName. For every non-root task, indent by two spaces for each ancestor before its parent, then write |- when the task has a later sibling or \- when it is its parent's final child, followed by one space, the task ID, one space, and the task name.

Return one string per rendered line.

Function

renderTaskTree(rows: String[]) → String[]

Examples

Example 1

rows = ["01/01/2025,task,T1,cook dinner","01/01/2025,subtask,T1,T2,buy groceries"]return = ["T1 cook dinner","\\- T2 buy groceries"]

T1 is a root. Its only child T2 is also its final child, so the second line uses \-.

Example 2

rows = ["01/01/2025,task,T1,plan launch","01/01/2025,subtask,T1,T2,write brief","01/01/2025,subtask,T1,T3,build demo","01/01/2025,subtask,T3,T4,record video","01/01/2025,task,T5,review metrics"]return = ["T1 plan launch","|- T2 write brief","\\- T3 build demo","  \\- T4 record video","T5 review metrics"]

T2 has a later sibling and uses |-. T3 is the final child of T1, while its child T4 is indented by one two-space ancestor prefix.

Example 3

rows = ["2025-01-01,task,A,alpha","2025-01-01,task,B,beta","2025-01-01,subtask,B,C,gamma","2025-01-01,subtask,B,D,delta","2025-01-01,subtask,D,E,epsilon"]return = ["A alpha","B beta","|- C gamma","\\- D delta","  \\- E epsilon"]

The two roots preserve input order. Under B, C is nonfinal, D is final, and E is the final child one level deeper.

Constraints

  • 1 <= rows.length <= 200000.
  • Every row is valid unquoted ASCII CSV in one of the two documented shapes.
  • Every taskId is unique and every referenced parentId already exists.
  • The rows describe an acyclic forest with nesting depth at most 200.
  • Task IDs and names are non-empty and contain no comma.

More Stripe problems

drafts saved locally
public String[] renderTaskTree(String[] rows) {
  // write your code here
}
rows["01/01/2025,task,T1,cook dinner","01/01/2025,subtask,T1,T2,buy groceries"]
expected["T1 cook dinner", "\\- T2 buy groceries"]
checking account