FastPrepBuild a Task and Subtask Hierarchy

Build a Task and Subtask Hierarchy

Stripe logoStripe● MediumFULLTIMENEW GRADINTERNPHONE SCREEN

Problem statement

A friend shared that this problem came up in a Stripe interview in three steps: build the task hierarchy (Task 1), print it with branch markers (Task 2), then mark each task's last child (Task 2b). Only the Task 2b output is judged.

Input format

The interview gave no function stub: candidates read standard input until EOF and parsed every line themselves. Here, lines holds those input lines in the order they were read, each with its line ending removed. The lines are unordered task and subtask records with comma-separated fields:

  • A task line is date,task,taskId,name.
  • A subtask line is date,subtask,subtaskId,parentTaskId,name.

Split each line on commas only. Do not trim fields or split on spaces, because names can contain spaces. The second field alone decides whether a line is a task or a subtask.

Task 1: Build the hierarchy

Separate task records from subtask records, and group every subtask under the task whose ID equals its parentTaskId. A subtask can appear before its parent, so read all lines before printing anything.

Order tasks by (date, taskId), and order the subtasks under each task by (date, subtaskId). Compare dates and IDs as plain strings, character by character. For example, t10 comes before t2.

Task 2: Print each child with a branch marker

Print each task's name on its own line, followed by the names of its direct subtasks. Prefix every subtask name with |- (a vertical bar, a hyphen, and one space). Print names only, with no IDs, dates, indentation, or blank lines. For the first example:

build
|- code
|- review
ship

Task 2b: Mark the last child

Change the marker of each task's last subtask, after sorting, to \- (a backslash, a hyphen, and one space). Every other subtask keeps |- . A task with one subtask uses \- for it, and a task with no subtasks prints only its name. Task names never get a marker.

build
|- code
\- review
ship

Return the Task 2b output as an array with one string per printed line.

Rehearse the stdin setup

To practice the interview's setup, wrap your solution in a standalone program: read sys.stdin line by line (or call input() until it raises EOFError), remove only each line ending, and print every returned line. The editorial includes a complete Python version.

Function

printTaskHierarchy(lines: String[]) → String[]

Examples

Example 1

lines = ["2026-01-03,subtask,s2,t1,review","2026-01-02,task,t2,ship","2026-01-01,task,t1,build","2026-01-02,subtask,s1,t1,code"]return = ["build","|- code","\\- review","ship"]

Task build (2026-01-01) prints before ship (2026-01-02). Under build, code (2026-01-02) comes before review (2026-01-03), so review is the last child and uses \-. ship has no subtasks, so it prints alone.

Example 2

lines = ["2026-02-01,task,b,Beta","2026-02-01,subtask,x2,a,write tests","2026-02-01,task,a,Alpha","2026-02-01,subtask,x1,a,draft spec","2026-02-03,subtask,y1,b,deploy"]return = ["Alpha","|- draft spec","\\- write tests","Beta","\\- deploy"]

Both tasks share a date, so ID a puts Alpha first. Alpha's two subtasks also share a date, so IDs x1 and x2 order them. Beta has one subtask, which is also its last child and uses \-.

Constraints

  • 0 <= lines.length <= 200.
  • Each line has exactly one of the two comma-separated shapes, and its second field is exactly task or subtask.
  • Dates use zero-padded YYYY-MM-DD format.
  • All task and subtask IDs are unique, and every parentTaskId names a task in lines.
  • IDs contain 1 to 20 letters, digits, hyphens, or underscores.
  • Names contain 1 to 50 letters, digits, spaces, hyphens, or underscores, and do not start or end with a space.
  • No field contains a comma, and only direct task-to-subtask relationships are present.

More Stripe problems

See Stripe hiring insights
Saved
public String[] printTaskHierarchy(String[] lines) {
    // Write your solution here.
}
lines["2026-01-03,subtask,s2,t1,review","2026-01-02,task,t2,ship","2026-01-01,task,t1,build","2026-01-02,subtask,s1,t1,code"]
expected["build", "|- code", "\\- review", "ship"]
Checking account…