FastPrepFastPrep
Problem Brief

Assign Server Numbers by Type

FULLTIMEOA

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.

Function Description

Complete the function assignServerNumbersByType in the editor below.

assignServerNumbersByType has the following parameter:

  1. String[] requests: the list of allocation and deallocation requests

Returns

int[]: the server numbers assigned by the allocation requests, in order.

1Example 1

Input
requests = ["allocate db", "allocate db", "allocate cache", "deallocate db 1", "allocate db", "allocate cache", "deallocate cache 1", "allocate cache"]
Output
[1, 2, 1, 1, 2, 1]
Explanation

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

Input
requests = ["allocate gpu", "deallocate gpu 1", "allocate gpu", "allocate api", "allocate gpu", "deallocate api 1", "allocate api"]
Output
[1, 1, 1, 2, 1]
Explanation

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.
public int[] assignServerNumbersByType(String[] requests) {
    // write your code here
}
Input

requests

["allocate db", "allocate db", "allocate cache", "deallocate db 1", "allocate db", "allocate cache", "deallocate cache 1", "allocate cache"]

Output

[1, 2, 1, 1, 2, 1]

Sign in to submit your solution.