Assign Server Numbers by Type
You are given a list of server-management requests. Each request is either "allocate <type>" or "deallocate <type> <id>".
Every server type has its own numbering space starting from 1. When an allocate request arrives for a type, assign the smallest positive server number that is currently unused for that type. When a deallocate request arrives, that server number becomes available again for future requests of the same type.
Return the server numbers assigned by all allocate requests, in the order those allocation requests appear.
Complete the function assignServerNumbersByType in the editor below.
assignServerNumbersByType has the following parameter:
String[] requests: the list of allocation and deallocation requests
Returns
int[]: the server numbers assigned by the allocation requests, in order.
1Example 1
The first two db allocations receive 1 and 2. The first cache allocation receives 1. After db 1 is released, the next db allocation reuses 1. The cache pool behaves independently, so its next allocations are 2 and then 1 again after cache 1 is released.
2Example 2
Each server type keeps its own smallest-available-id pool. The gpu allocations produce 1, then reuse 1 after deallocation, and later allocate 2. The api type is independent, so it allocates 1, releases it, and then allocates 1 again.
Constraints
Limits and guarantees your solution can rely on.
The source thread did not provide explicit numeric bounds.
- Each request is either
"allocate <type>"or"deallocate <type> <id>". - Server numbers are positive integers.
- Every deallocation request refers to a server number that is currently allocated for that same type.