FastPrepLicense Checkout and Return Manager

License Checkout and Return Manager

Headway logoHeadway● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a finite license checkout manager. A pool of capacity licenses is shared by users. Process each string in operations in order; it is either checkout,userId or return,userId.

For this exercise, assume licenses are numbered 1 through capacity and each user may hold at most one license. Checkout returns the user's existing license when already leased; otherwise it assigns the smallest available license. When none is free, return -1.

For this exercise, assume return releases the requesting user's current license and returns its identifier. If that user has no license, return -1 without changing the pool. Operations are sequential and licenses do not expire.

For this exercise, assume operations are valid, contain exactly one comma without spaces, and user identifiers are nonempty case-sensitive alphanumeric strings. Return one integer for each operation; an empty sequence produces an empty result.

Function

licenseOperations(capacity: int, operations: String[]) → int[]

Examples

Example 1

capacity = 2operations = ["checkout,amy","checkout,bob","checkout,cat","return,amy","checkout,cat"]return = [1,2,-1,1,1]

Amy receives1, Bob2, and the full pool rejects Cat. Returning Amy frees1 for Cat.

Example 2

capacity = 1operations = ["checkout,u","checkout,u","return,u","return,u"]return = [1,1,1,-1]

Repeat checkout reuses the same lease; after one successful return, another return has no lease to release.

Constraints

  • For this exercise, assume 0 <= capacity <= 10^4.
  • 0 <= operations.length <= 10^4.
  • User identifier lengths are from 1 through 30.
  • Actions are exactly checkout or return.

More Headway problems

See Headway hiring insights
public int[] licenseOperations(int capacity, String[] operations) {
    // Write your code here.
    return new int[0];
}
capacity2
operations["checkout,amy","checkout,bob","checkout,cat","return,amy","checkout,cat"]
expected[1,2,-1,1,1]
Checking account…