Design Task Manager - Problem
Design and implement a Task Manager system that efficiently handles task operations across multiple users.
Each task has a taskId, belongs to a userId, and has a priority. The system must support:
- Adding new tasks
- Editing task priorities
- Removing tasks
- Executing the highest priority task (highest
taskIdfor ties)
Implement the TaskManager class with the following methods:
TaskManager(tasks)- Initialize with list of[userId, taskId, priority]triplesadd(userId, taskId, priority)- Add new task (guaranteed uniquetaskId)edit(taskId, newPriority)- Update existing task priorityrmv(taskId)- Remove existing taskexecTop()- Execute and remove highest priority task, return itsuserId(return -1 if no tasks)
Input & Output
Example 1 — Basic Operations
$
Input:
[["TaskManager", [[1,101,2],[1,102,5]]], ["add", 2,103,1], ["execTop"], ["execTop"]]
›
Output:
[null, null, 1, 1]
💡 Note:
Initialize with 2 tasks. Add task 103. execTop() returns user 1 (task 102, priority 5). execTop() returns user 1 (task 101, priority 2).
Example 2 — Edit and Remove
$
Input:
[["TaskManager", [[2,201,3],[2,202,1]]], ["edit", 202,5], ["execTop"], ["rmv", 201], ["execTop"]]
›
Output:
[null, null, 2, null, -1]
💡 Note:
Edit task 202 priority to 5. execTop() returns user 2 (task 202). Remove task 201. execTop() returns -1 (no tasks).
Example 3 — Priority Ties
$
Input:
[["TaskManager", [[1,301,2],[2,302,2]]], ["execTop"]]
›
Output:
[null, 2]
💡 Note:
Both tasks have priority 2, but task 302 has higher taskId, so execTop() returns user 2.
Constraints
- 1 ≤ tasks.length ≤ 105
- 1 ≤ userId, taskId ≤ 105
- 1 ≤ priority ≤ 106
- At most 5 × 104 calls to add, edit, rmv, and execTop
Visualization
Tap to expand
Understanding the Visualization
1
Input
Operations list with TaskManager initialization and method calls
2
Process
Execute operations using hash map + priority queue structure
3
Output
Return results array with null for void methods and userIds for execTop
Key Takeaway
🎯 Key Insight: Combine hash map's O(1) lookups with priority queue's O(log n) ordering for optimal performance
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code