Reusable Number Allocator
Updated
Problem statement
Implement a reusable allocator for the integers from 0 through n - 1.
The allocator starts with every number available. Process the parallel arrays operations and values from left to right:
assignreturns and reserves the smallest currently available number. If no number is available, it returns-1. Its corresponding entry invaluesis-1.releasemakesvalues[i]available again. Every release is guaranteed to name a number that is currently assigned. A release produces no output.
Return the results of all assign operations in operation order.
Function
runNumberAllocator(n: int, operations: String[], values: int[]) → int[]Examples
Example 1
n = 3operations = ["assign","assign","release","assign","assign"]values = [-1,-1,0,-1,-1]return = [0,1,0,2]The first two assignments reserve 0 and 1. Releasing 0 makes it the smallest available number again, so the next assignment reuses it before assigning 2.
Example 2
n = 2operations = ["assign","assign","assign","release","assign"]values = [-1,-1,-1,1,-1]return = [0,1,-1,1]After 0 and 1 are assigned, the allocator is exhausted and returns -1. Releasing 1 lets the final assignment return 1.
Constraints
1 <= n <= 10^61 <= operations.length <= 2 * 10^5values.length == operations.length- Each operation is exactly
assignorrelease. - For
assign, the corresponding value is-1. - For
release,0 <= values[i] < nand that number is currently assigned.
More Dropbox interview questions
Similar questions at other companies
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Delayed Job SchedulerWalmart Labs · ONSITE INTERVIEW
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Notification Deduplication WindowGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Implement a Min-Priority QueueAmazon · ONSITE INTERVIEW
- Helpdesk Ticket ProcessingThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Execute Tasks by PrioritySnowflake · ONSITE INTERVIEWSnowflake interview questions
- Priority Job Scheduler with CooldownsGoogle · ONSITE INTERVIEWGoogle interview questions
- Order Matching EngineJane Street · ONSITE INTERVIEWJane Street interview questions
- Trip Booking with the Earliest Free DriverRippling · ONSITE INTERVIEWRippling interview questions