FastPrepID Allocator with Assign and Release
Problems/ID Allocator with Assign and Release

ID Allocator with Assign and Release

Snap Inc. logoSnap Inc.● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In a Snap phone screen, after a behavioral part, the coding question was an ID allocator for the IDs 0 to N-1. One call hands out a free ID, or -1 when none is left, and another call frees an ID. The report says to check that N is valid, and that a freed ID is in range and really in use. A second part, about an N too large to keep in memory, was only discussed.

The report lets assign return any free ID, gives release no result, and does not say what a bad release or a bad N should do. Here, assign always returns the smallest free ID so that answers can be checked, a release returns 1 when it frees an ID, a bad release returns 0 and changes nothing, an N of 0 or less has no IDs, and the limits and examples are ours. You get the calls as a list of operations instead of a class. The large-N part is covered in the editorial, not judged.

The judged task matches the report at about 80%.

An ID allocator manages the IDs 0, 1, ..., n - 1. At the start every ID is free. Process the operations operations[0], operations[1], ... in order, and record one result for each:

  • "assign": hand out the smallest free ID, mark it as in use, and record it. If every ID is in use, nothing changes and the result is -1.
  • "release": values[i] is an ID. If it is between 0 and n - 1 and is in use, mark it as free and record 1. Otherwise (it is outside that range, or it is already free), nothing changes and the result is 0.

If n <= 0, the allocator has no valid IDs: every assign records -1 and every release records 0.

values[i] is only used by a release; for an assign it is 0. Return the recorded results in order.

Function

runIdAllocator(n: int, operations: String[], values: int[]) → int[]

Examples

Example 1

n = 3operations = ["assign","assign","release","assign","assign","assign","release"]values = [0,0,0,0,0,0,5]return = [0,1,1,0,2,-1,0]
  • The first two assign calls hand out the smallest free IDs, 0 and 1.
  • release 0 frees ID 0, so it records 1.
  • The next assign returns 0 again, because it is now the smallest free ID. The one after that returns 2.
  • All three IDs are in use, so the last assign records -1.
  • release 5 names an ID outside 0 to 2, so it records 0.

Example 2

n = 2operations = ["release","assign","release","release","assign"]values = [1,0,0,0,0]return = [0,0,1,0,0]
  • release 1 comes before ID 1 was ever handed out, so it records 0.
  • assign returns 0, and release 0 frees it and records 1.
  • The second release 0 finds ID 0 already free, so it records 0 and changes nothing.
  • The last assign returns 0.

Example 3

n = 0operations = ["assign","release"]values = [0,0]return = [-1,0]

With n = 0 there are no valid IDs. assign records -1 and release 0 records 0.

Constraints

  • -2000 <= n <= 2000
  • 1 <= operations.length <= 2000
  • values.length == operations.length
  • Each operations[i] is "assign" or "release".
  • -10^9 <= values[i] <= 10^9
  • values[i] == 0 whenever operations[i] is "assign".

More Snap Inc. problems

  • Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
  • Basic Calculator IIPHONE SCREEN · Seen Oct 2026
  • Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
All Snap Inc. interview questionsSee Snap Inc. hiring insights
Saved
class Solution {
    public int[] runIdAllocator(int n, String[] operations, int[] values) {
        // Write your code here.
    }
}
Checking account…