FastPrepAllocate and Reuse Server Names by Type

Allocate and Reuse Server Names by Type

Stripe logoStripe● EasyINTERNPHONE SCREEN
Learn

Problem statement

Process server-management requests. Every server type has an independent numbering space that starts at 1.

  • allocate <type>: reserve the smallest positive number not currently used for that type and return the hostname formed by concatenating the type and number.
  • deallocate <hostname>: release that active hostname so its number can be reused by a later allocation of the same type.

Return the hostnames produced by all allocation requests, in request order.

What the interview report shared

The source first asked for the smallest missing positive integer, then reused that capability to allocate the minimum available numbered hostname per type and release names for reuse. The examples showed duplicate input numbers and independent numbering across types.

Function

allocateServerNames(requests: String[]) → String[]

Examples

Example 1

requests = ["allocate api", "allocate api", "allocate site", "deallocate api1", "allocate api"]return = ["api1", "api2", "site1", "api1"]

The api type allocates 1 and 2. The site type starts its own numbering at 1. After api1 is released, the next API allocation reuses it.

Example 2

requests = ["allocate db", "deallocate db1", "deallocate db1", "allocate db", "allocate db"]return = ["db1", "db1", "db2"]

The second deallocation is ignored because db1 is already free. The next allocation reuses db1, and the following one receives db2.

Constraints

  • 1 <= requests.length <= 10^5.
  • Server types contain lowercase English letters.
  • Allocated server numbers are positive integers.
  • Every request is either allocate <type> or deallocate <hostname>.
  • For this exercise, assume an invalid or already released hostname is ignored.

More Stripe problems

See Stripe hiring insights
public String[] allocateServerNames(String[] requests) {
    // write your code here
}
requests["allocate api", "allocate api", "allocate site", "deallocate api1", "allocate api"]
expected["api1", "api2", "site1", "api1"]
Checking account…