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: Input → Operations → OutputInput OperationsTaskManager([[1,101,2]])add(2,102,5)execTop()Task Manager SystemHash Map: {101→(1,2), 102→(2,5)}Priority Queue: [(5,102), (2,101)]execTop() → User 2Output Results[null,null,2]Operations execute in O(log n) time using efficient data structuresHash Map enables O(1) lookups, Priority Queue maintains order
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
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