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 triples
  • add(userId, taskId, priority) - Add a new task to a user
  • edit(taskId, newPriority) - Update a task's priority
  • rmv(taskId) - Remove a task completely
  • execTop() - 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
Task Manager System ArchitecturePriority Queue (Max Heap)T4:P5T2:P5T1:P3T3:P1Hash Map (Fast Lookup)taskId: 1 โ†’ {userId: 1, priority: 3, valid: true}taskId: 2 โ†’ {userId: 2, priority: 5, valid: true}taskId: 3 โ†’ {userId: 1, priority: 1, valid: true}taskId: 4 โ†’ {userId: 3, priority: 5, valid: true}O(1) ValidationexecTop() Workflow1Pop max from heap: (priority=5, taskId=4, userId=3)2Lookup taskId=4 in hash map: valid=true, priority matches3Return userId=3, remove task from systemโšก Time Complexity: O(log n) for heap, O(1) for hash lookupLazy deletion ensures consistency between data structures
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.
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 25 Apple 18
73.9K Views
High Frequency
~32 min Avg. Time
1.8K 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