Problem · Design

Design Task Manager

Learn this problem
MediumApple logoAppleFULLTIMEPHONE SCREEN

Problem statement

Manage tasks owned by users. Every task has a unique integer taskId, an integer userId, and an integer priority. The initial array tasks contains rows [userId, taskId, priority].

For this exercise, process the finite batch operations. Each row is one of:

  • ["ADD", userId, taskId, priority]: add a task. The supplied taskId is not currently present.
  • ["EDIT", taskId, newPriority]: change the priority of an existing task.
  • ["RMV", taskId]: remove an existing task.
  • ["EXEC_TOP"]: execute and remove the task with the greatest priority. If several tasks have that priority, execute the one with the greatest taskId. Return its userId, or -1 when no task is available.

The numeric fields in operations are decimal strings. Return the result of every EXEC_TOP operation in order.

Function

runTaskManager(tasks: int[][], operations: String[][]) → int[]

Examples

Example 1

tasks = [[1,101,10],[2,102,20],[3,103,15]]operations = [["ADD","4","104","5"],["EDIT","102","8"],["EXEC_TOP"],["RMV","101"],["ADD","5","105","15"],["EXEC_TOP"]]return = [3,5]

After task 102 is lowered to priority 8, task 103 is the first task executed, so the first result is user 3. Task 101 is removed, and newly added task 105 is then the highest-priority task, so the second result is user 5.

Example 2

tasks = [[1,10,5],[2,20,5]]operations = [["EXEC_TOP"],["EDIT","10","7"],["EXEC_TOP"],["EXEC_TOP"]]return = [2,1,-1]

The initial priority tie is broken by the larger task ID, so task 20 returns user 2. Editing task 10 makes it the next result. The final execution finds no task and returns -1.

Constraints

  • 1 <= tasks.length <= 10^5
  • Every initial row is [userId, taskId, priority], and initial task IDs are unique.
  • 0 <= userId, taskId <= 10^5
  • 0 <= priority, newPriority <= 10^9
  • 0 <= operations.length <= 2 * 10^5
  • Every operation has a valid name and arity.
  • An ADD task ID is absent when added; an EDIT or RMV task ID is present when used.

More Apple problems

drafts saved locally
public int[] runTaskManager(int[][] tasks, String[][] operations) {
    // Write your code here.
}
tasks[[1,101,10],[2,102,20],[3,103,15]]
operations[["ADD","4","104","5"],["EDIT","102","8"],["EXEC_TOP"],["RMV","101"],["ADD","5","105","15"],["EXEC_TOP"]]
expected[3,5]
checking account