Build the Task Hierarchy
Updated
Problem statement
You are given a flat list of tasks. Each task is either a ParentTask or a SubTask. Each row of tasks describes one task with seven fields, in this order: [id, name, isExpanded, createdAt, updatedAt, isCompleted, parentId].
id: the unique identifier of the task.name: the task description.isExpanded: a UI state, whether the task is expanded in the UI. It is"true"or"false".createdAtandupdatedAt: times written asYYYY-MM-DDTHH:MM:SSZ.isCompleted: the completion state,"true"or"false".parentId: if it is the empty string"", the task is a ParentTask. Otherwise the task is a SubTask, andparentIdis the id of its parent task.
Turn the flat list into a nested structure in three parts:
- Build the hierarchy. The result is an array that contains only ParentTasks at the top level. Each ParentTask has a
subTasksarray with all of its SubTasks nested inside. A SubTask whoseparentIddoes not match the id of any task intaskshas no parent to go under, so it is left out. - Recompute the completion status of each ParentTask. If a ParentTask has at least one SubTask, its
isCompletedistruewhen all of its SubTasks are completed andfalseotherwise. A ParentTask with no SubTasks keeps its ownisCompletedvalue. - Sort the output. Sort the ParentTasks by
createdAtin ascending order (oldest first), and sort the SubTasks in eachsubTasksarray the same way. Tasks with the samecreatedAtkeep their order fromtasks.
Return the result as a JSON string with no spaces or line breaks. A ParentTask object has the keys id, name, isExpanded, createdAt, updatedAt, isCompleted, parentId and subTasks, in this order. A SubTask object has the same first seven keys and no subTasks key. isExpanded and isCompleted are written as JSON booleans (true or false, without quotes), and the other fields as JSON strings. Every value is copied from the input unchanged, except the isCompleted value of a ParentTask in part 2. A ParentTask with no SubTasks has "subTasks":[], and if there is no ParentTask the answer is "[]".
Function
buildTaskHierarchy(tasks: String[][]) → StringExamples
Example 1
tasks = [["st021","Design auth flow","false","2026-01-05T09:12:00Z","2026-01-06T10:00:00Z","true","t004"],["st004","Create Firebase project","false","2026-01-01T10:15:00Z","2026-01-02T09:00:00Z","true","t001"],["t001","Setup project auth","true","2026-01-01T09:00:00Z","2026-01-02T12:00:00Z","true",""],["st030","Seed test data","false","2026-01-06T10:10:00Z","2026-01-07T11:00:00Z","false","t006"]]return = "[{\"id\":\"t001\",\"name\":\"Setup project auth\",\"isExpanded\":true,\"createdAt\":\"2026-01-01T09:00:00Z\",\"updatedAt\":\"2026-01-02T12:00:00Z\",\"isCompleted\":true,\"parentId\":\"\",\"subTasks\":[{\"id\":\"st004\",\"name\":\"Create Firebase project\",\"isExpanded\":false,\"createdAt\":\"2026-01-01T10:15:00Z\",\"updatedAt\":\"2026-01-02T09:00:00Z\",\"isCompleted\":true,\"parentId\":\"t001\"}]}]"Task t001 has an empty parentId, so it is the only ParentTask. st004 names t001 as its parent, so it goes into the subTasks array of t001. st021 and st030 name t004 and t006, which are not in the list, so they are left out. The only SubTask of t001 is completed, so t001 stays completed (true).
Example 2
tasks = [["t2","Write docs","false","2026-03-02T08:00:00Z","2026-03-04T09:00:00Z","true",""],["s21","Draft outline","false","2026-03-03T09:30:00Z","2026-03-03T17:00:00Z","true","t2"],["t1","Ship release","true","2026-03-01T10:00:00Z","2026-03-01T18:00:00Z","false",""],["s22","Add examples","false","2026-03-02T12:00:00Z","2026-03-02T12:00:00Z","false","t2"],["s12","Run tests","false","2026-03-01T15:45:00Z","2026-03-01T16:30:00Z","true","t1"],["s11","Freeze branch","true","2026-03-01T11:20:00Z","2026-03-01T11:25:00Z","true","t1"]]return = "[{\"id\":\"t1\",\"name\":\"Ship release\",\"isExpanded\":true,\"createdAt\":\"2026-03-01T10:00:00Z\",\"updatedAt\":\"2026-03-01T18:00:00Z\",\"isCompleted\":true,\"parentId\":\"\",\"subTasks\":[{\"id\":\"s11\",\"name\":\"Freeze branch\",\"isExpanded\":true,\"createdAt\":\"2026-03-01T11:20:00Z\",\"updatedAt\":\"2026-03-01T11:25:00Z\",\"isCompleted\":true,\"parentId\":\"t1\"},{\"id\":\"s12\",\"name\":\"Run tests\",\"isExpanded\":false,\"createdAt\":\"2026-03-01T15:45:00Z\",\"updatedAt\":\"2026-03-01T16:30:00Z\",\"isCompleted\":true,\"parentId\":\"t1\"}]},{\"id\":\"t2\",\"name\":\"Write docs\",\"isExpanded\":false,\"createdAt\":\"2026-03-02T08:00:00Z\",\"updatedAt\":\"2026-03-04T09:00:00Z\",\"isCompleted\":false,\"parentId\":\"\",\"subTasks\":[{\"id\":\"s22\",\"name\":\"Add examples\",\"isExpanded\":false,\"createdAt\":\"2026-03-02T12:00:00Z\",\"updatedAt\":\"2026-03-02T12:00:00Z\",\"isCompleted\":false,\"parentId\":\"t2\"},{\"id\":\"s21\",\"name\":\"Draft outline\",\"isExpanded\":false,\"createdAt\":\"2026-03-03T09:30:00Z\",\"updatedAt\":\"2026-03-03T17:00:00Z\",\"isCompleted\":true,\"parentId\":\"t2\"}]}]"The ParentTasks are t1, created on March 1, and t2, created on March 2, so t1 comes first even though it is listed after t2. Under t1, s11 (11:20) comes before s12 (15:45). Both are completed, so t1 changes from false to true. Under t2, s22 (March 2) comes before s21 (March 3). s22 is not completed, so t2 changes from true to false.
Example 3
tasks = [["b","Plan sprint","false","2026-05-04T09:00:00Z","2026-05-04T09:00:00Z","true",""],["a","Review code","true","2026-05-04T09:00:00Z","2026-05-05T10:00:00Z","false",""],["a2","Check tests","false","2026-05-04T13:00:00Z","2026-05-04T15:00:00Z","true","a"],["a1","Check style","false","2026-05-04T13:00:00Z","2026-05-04T14:00:00Z","false","a"],["c","Clean backlog","false","2026-05-01T08:00:00Z","2026-05-02T08:00:00Z","false",""]]return = "[{\"id\":\"c\",\"name\":\"Clean backlog\",\"isExpanded\":false,\"createdAt\":\"2026-05-01T08:00:00Z\",\"updatedAt\":\"2026-05-02T08:00:00Z\",\"isCompleted\":false,\"parentId\":\"\",\"subTasks\":[]},{\"id\":\"b\",\"name\":\"Plan sprint\",\"isExpanded\":false,\"createdAt\":\"2026-05-04T09:00:00Z\",\"updatedAt\":\"2026-05-04T09:00:00Z\",\"isCompleted\":true,\"parentId\":\"\",\"subTasks\":[]},{\"id\":\"a\",\"name\":\"Review code\",\"isExpanded\":true,\"createdAt\":\"2026-05-04T09:00:00Z\",\"updatedAt\":\"2026-05-05T10:00:00Z\",\"isCompleted\":false,\"parentId\":\"\",\"subTasks\":[{\"id\":\"a2\",\"name\":\"Check tests\",\"isExpanded\":false,\"createdAt\":\"2026-05-04T13:00:00Z\",\"updatedAt\":\"2026-05-04T15:00:00Z\",\"isCompleted\":true,\"parentId\":\"a\"},{\"id\":\"a1\",\"name\":\"Check style\",\"isExpanded\":false,\"createdAt\":\"2026-05-04T13:00:00Z\",\"updatedAt\":\"2026-05-04T14:00:00Z\",\"isCompleted\":false,\"parentId\":\"a\"}]}]"c is the oldest ParentTask. b and a have the same createdAt, so they keep their input order: b before a. c and b have no SubTasks, so they keep their own isCompleted values, false and true, and get an empty subTasks array. The two SubTasks of a also share a createdAt, so a2 stays before a1. a1 is not completed, so a is false.
Constraints
0 <= tasks.length <= 50tasks[i].length == 70 <= tasks[i][j].length <= 30- Every
idis unique and has 1 to 10 letters or digits. namehas 1 to 30 characters, and each one is a letter, a digit or a space.isExpandedandisCompletedare each"true"or"false".createdAtandupdatedAtare valid UTC times written asYYYY-MM-DDTHH:MM:SSZ, with a year from 2000 to 2099.parentIdis""or 1 to 10 letters or digits. It is never the task's ownid, and it never names a SubTask, so there are only two levels.
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
- Items SortAkuna Capital · OAAkuna Capital interview questions
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Array Rank TransformTata Consultancy Services · OATata Consultancy Services interview questions
- Find Max Health SumAmazon · OA
- Influencers SquadTikTok · OATikTok interview questions
- Count Teams Outscoring a Target TeamBloomberg LP · OABloomberg LP interview questions
- Count Soldiers with a Next-Rank SuperiorMarshall Wace · OAMarshall Wace interview questions