Design Task Manager - Problem
You're building a Task Management System that handles tasks across multiple users, each with different priorities. Think of it like a sophisticated to-do list manager for a team where the system needs to efficiently track, modify, and execute tasks based on their importance.
Your system should support the following operations:
TaskManager(tasks)- Initialize with existing user-task-priority triplesadd(userId, taskId, priority)- Add a new task to a useredit(taskId, newPriority)- Update a task's priorityrmv(taskId)- Remove a task completelyexecTop()- Execute the highest priority task (highest taskId breaks ties)
Key Challenge: When executing tasks, you need to find the highest priority task across all users. If multiple tasks have the same priority, pick the one with the highest taskId.
Example: If User 1 has task 100 (priority 5) and User 2 has task 200 (priority 5), executing the top task should return User 2 since task 200 > task 100.
Input & Output
basic_operations.py โ Python
$
Input:
TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]])
tm.add(1, 104, 25)
tm.execTop()
tm.edit(103, 30)
tm.execTop()
โบ
Output:
[1, 3]
๐ก Note:
Initially we have tasks: T101(P10,U1), T102(P20,U2), T103(P15,U3). After adding T104(P25,U1), execTop() returns 1 (User1 owns highest priority task T104). After editing T103 to priority 30, execTop() returns 3 (User3 owns T103 with priority 30).
tie_breaking.py โ Python
$
Input:
TaskManager([[1, 50, 10], [2, 100, 10]])
tm.execTop()
โบ
Output:
2
๐ก Note:
Both tasks have priority 10, but task 100 > task 50, so User2 wins the tie-breaker.
remove_and_empty.py โ Python
$
Input:
TaskManager([[1, 201, 5]])
tm.rmv(201)
tm.execTop()
โบ
Output:
-1
๐ก Note:
After removing the only task, execTop() returns -1 since no tasks remain.
Constraints
- 1 โค tasks.length โค 105
- 1 โค userId, taskId โค 105
- 0 โค priority โค 109
- At most 2 ร 105 calls will be made to add, edit, rmv, and execTop
- taskId is unique across the entire system
- Guaranteed valid inputs for edit and rmv operations
Visualization
Tap to expand
Understanding the Visualization
1
Data Structures
Max heap for priority queue + hash map for fast lookups
2
Add Operation
Insert into both heap and hash map in O(log n) time
3
Edit Operation
Mark old entry invalid, add new entry with updated priority
4
Execute Top
Pop from heap, verify validity, return user ID
Key Takeaway
๐ฏ Key Insight: Combining heap for priority ordering with hash map for fast lookups gives us the best of both worlds - efficient priority operations and constant-time task management.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code