FastPrepBuild a Task and Subtask Hierarchy
Problem · Array

Build a Task and Subtask Hierarchy

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

You are given an unordered array of task and subtask records.

  • A task record is [date, "task", taskId, name].
  • A subtask record is [date, "subtask", subtaskId, parentTaskId, name].

First separate task records from subtask records. Then serialize the hierarchy by ordering tasks by (date, taskId). Emit each task followed immediately by its direct subtasks ordered by (date, subtaskId).

Serialize a task as TASK|taskId|name and a subtask as SUBTASK|subtaskId|name. Return the emitted lines.

Function

buildTaskHierarchy(records: String[][]) → String[]

Examples

Example 1

records = [["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 = ["TASK|t1|build","SUBTASK|s1|code","SUBTASK|s2|review","TASK|t2|ship"]

Task t1 is earlier than t2. Its two subtasks follow it in date order even though every input record is unordered.

Example 2

records = [["2026-02-01","task","b","Beta"],["2026-02-01","task","a","Alpha"]]return = ["TASK|a|Alpha","TASK|b|Beta"]

Equal task dates break ties by task ID. Tasks with no subtasks still appear.

Constraints

  • 0 <= records.length <= 200.
  • Every record has one of the two stated shapes and uses the exact lowercase marker task or subtask.
  • Dates use zero-padded YYYY-MM-DD format.
  • All task and subtask IDs are unique, and every subtask names an existing task.
  • IDs and names contain 1 to 50 letters, digits, spaces, hyphens, or underscores and do not contain |.
  • Only direct task-to-subtask relationships are present.

More Stripe problems

drafts saved locally
public String[] buildTaskHierarchy(String[][] records) {
    // Write your solution here.
}
records[["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["TASK|t1|build", "SUBTASK|s1|code", "SUBTASK|s2|review", "TASK|t2|ship"]
checking account