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 taskId for ties)

Implement the TaskManager class with the following methods:

  • TaskManager(tasks) - Initialize with list of [userId, taskId, priority] triples
  • add(userId, taskId, priority) - Add new task (guaranteed unique taskId)
  • edit(taskId, newPriority) - Update existing task priority
  • rmv(taskId) - Remove existing task
  • execTop() - Execute and remove highest priority task, return its userId (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
Task Manager System Design INPUT Initial Tasks: userId taskId priority status 1 101 2 active 1 102 5 active Operations Sequence: 1. TaskManager(tasks) 2. add(2, 103, 1) 3. execTop() 4. execTop() Hash Map + Priority Queue HashMap MaxHeap ALGORITHM STEPS 1 Initialize Build HashMap & MaxHeap Key: taskId, Val: Task obj 2 Add Task Insert into HashMap O(1) Push to MaxHeap O(log n) 3 Execute Top Pop max priority task Return userId, remove 4 Tie Breaking Same priority: higher taskId wins MaxHeap State (after add): p:5 p:2 p:1 FINAL RESULT Execution Trace: TaskManager() --> null Initialized with 2 tasks add(2,103,1) --> null Task 103 added for user 2 execTop() --> 1 Task 102 (p=5) executed execTop() --> 1 Task 101 (p=2) executed Output Array: [null, null, 1, 1] OK - Verified Remaining: Task 103 (user 2, p=1) Key Insight: HashMap provides O(1) lookup for task edit/remove operations by taskId. MaxHeap (Priority Queue) enables O(log n) insertion and O(1) access to highest priority task. For tie-breaking: heap comparator uses (priority, taskId) tuple - higher values win. TutorialsPoint - Design Task Manager | Hash Map + Priority Queue Time: add O(log n), edit O(log n), remove O(log n), execTop O(log n) | Space: O(n)
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
25.0K Views
High Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen