Filter, Sort, and Deduplicate Scheduled Tasks
Problem statement
A batch contains tasks described by parallel arrays. Task i has the unique ID taskIds[i], description descriptions[i], due date dueDates[i], priority priorities[i], completion flag completed[i], and parent ID parentIds[i]. An empty parent ID marks a root task.
Return the task IDs after applying these phases in order:
- Deduplicate: Tasks with the same exact pair
(description, due date)are duplicates. Keep only the first input occurrence of each pair. - Filter: Remove every retained task whose completion flag is
true. A task also survives only if its parent survives, so removing any task removes its entire descendant subtree. - Order: Compare retained tasks by higher priority first, then earlier due date, then earlier retained input position.
- Expand the hierarchy: Sort the root tasks and every sibling list with that comparison. Emit each root in order, followed immediately by its complete retained subtree in preorder.
Return each surviving task ID exactly once.
Function
orderHierarchicalTasks(taskIds: String[], descriptions: String[], dueDates: long[], priorities: int[], completed: boolean[], parentIds: String[]) → String[]Examples
Example 1
taskIds = ["p1","c1","dup","p2","c2","done","done-child"]descriptions = ["Launch","Docs","Launch","Payroll","Tax","Archive","Cleanup"]dueDates = [10,8,10,5,4,1,2]priorities = [2,5,9,3,7,10,9]completed = [false,false,false,false,false,true,false]parentIds = ["","p1","","","p2","","done"]return = ["p2","c2","p1","c1"]dup shares (Launch, 10) with the earlier task p1, so dup is discarded. Completed task done is removed together with descendant done-child. Priority places root p2 before p1, and each child follows its parent.
Example 2
taskIds = ["root2","root","a","b","grand"]descriptions = ["Other","Plan","Build","Review","Test"]dueDates = [2,20,10,5,1]priorities = [6,1,5,4,9]completed = [false,false,false,false,false]parentIds = ["","","root","root","a"]return = ["root2","root","a","grand","b"]Root root2 has higher priority than root. Inside root's subtree, a precedes sibling b; preorder emits nested task grand immediately after a before returning to b.
Example 3
taskIds = ["a","b","c"]descriptions = ["Sync","Sync","Done"]dueDates = [1,1,2]priorities = [1,9,5]completed = [true,false,true]parentIds = ["","",""]return = []Deduplication happens before filtering, so completed task a survives the duplicate pair and active task b is discarded. Filtering then removes a and c.
Constraints
0 <= taskIds.length <= 2 * 10^5.- All six arrays have the same length.
- Task IDs are unique strings containing
1to40ASCII letters, digits, hyphens, or underscores. - Descriptions contain
1to100visible ASCII characters; duplicate matching is exact and case-sensitive. - Each due date fits in a signed
64-bit integer, and each priority fits in a signed32-bit integer. - Each parent ID is empty or equals another input task ID, and the parent relation forms a forest.