Execute Tasks by Priority
Updated
Problem statement
Process a finite sequence of task-manager operations.
- An
addoperation inserts one task with its identifier, integer priority, and timestamp. - An
executeoperation removes and returns the pending task with the highest priority.
For this exercise, assume a larger integer means a higher priority. Break equal-priority ties by earlier timestamp, then by smaller task identifier. Task identifiers are unique, and a completed task cannot execute again. Return one value for each execute operation, in operation order; return -1 when no task is pending.
Function
executeTasks(operations: String[], taskIds: int[], priorities: int[], timestamps: int[]) → int[]Examples
Example 1
operations = ["add","add","execute","execute","execute"]taskIds = [101,102,0,0,0]priorities = [2,5,0,0,0]timestamps = [10,10,0,0,0]return = [102,101,-1]Task 102 has the higher priority, then task 101 runs. The final execution finds no pending task.
Example 2
operations = ["add","add","add","execute","execute"]taskIds = [9,4,7,0,0]priorities = [3,3,3,0,0]timestamps = [8,8,6,0,0]return = [7,4]Task 7 has the earliest timestamp. Tasks 4 and 9 then tie on timestamp, so the smaller identifier wins.
Constraints
1 <= operations.length <= 10^5- All four input arrays have the same length.
- Each operation is
addorexecute. - Task identifiers used by
addare unique positive integers. - Fields at an
executeposition are ignored.
More Snowflake interview questions
- Parallel Courses IIIONSITE INTERVIEW · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LFU CacheOA · ONSITE INTERVIEW · Seen Nov 2026
- Sliding-Window Rate LimiterONSITE INTERVIEW · Seen Sep 2026
- Priority Task Execution with Duplicate IDsONSITE INTERVIEW · Seen Aug 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Safest Shortest Path Through a GridOA · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Notification Deduplication WindowGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Helpdesk Ticket ProcessingThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Priority Job Scheduler with CooldownsGoogle · ONSITE INTERVIEWGoogle interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEWHudson River Trading interview questions
- Expiring Key SetAmazon · ONSITE INTERVIEW
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN