KatInterview

Smallest Free Label per Category: Batch Simulation

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designheaphash-mapsimulation

Last updated

Simulate a sequence of allocate and release operations on independent categories, always handing out the smallest free positive integer, using a counter plus a min-heap per category.

A data center hands out numeric labels to machines. Machines are grouped by a category (a string), and each category keeps its own independent numbering. Within a category, a new machine always receives the smallest positive integer (1, 2, 3, ...) that no active machine of that category currently holds. When a machine is decommissioned, its label becomes free and may be handed out again.

You are given a list of operations and a parallel list of arguments. Process them in order and return one result per operation.

Operations

  • "allocate" with arguments [category]: give out the smallest positive integer not currently in use in that category, mark it as in use, and record that integer as the result.
  • "release" with arguments [category, label]: mark label as free again in that category, and record None as the result.

Categories never affect each other: the label 1 in "web" is unrelated to the label 1 in "db". A category that has never been seen before starts with every positive integer free.

Task

Implement a function that takes operations (a list of strings) and arguments (a list of lists, where arguments[i] belongs to operations[i]) and returns a list of the same length. The i-th element is the label produced by the i-th operation if it is an "allocate", or None if it is a "release".

Constraints

  • len(operations) == len(arguments), and the total number of operations is at most 10^5.
  • Each operation is either "allocate" or "release".
  • A category is a non-empty string.
  • A label is a positive integer.
  • Every "release" refers to a label that was previously handed out in that category and is currently in use (no double releases, no releasing unknown labels).
  • The returned list must have exactly one entry per operation, using None for releases.

Examples

Example 1

Input: label_allocator(['allocate', 'allocate', 'release', 'allocate', 'allocate'], [['web'], ['web'], ['web', 1], ['web'], ['db']])

Output: [1, 2, None, 1, 1]

Example 2

Input: label_allocator(['allocate', 'allocate', 'allocate', 'release', 'release', 'allocate', 'allocate', 'allocate'], [['api'], ['api'], ['api'], ['api', 2], ['api', 1], ['api'], ['api'], ['api']])

Output: [1, 2, 3, None, None, 1, 2, 4]

Example 3

Input: label_allocator(['allocate', 'allocate', 'release', 'allocate', 'release', 'allocate'], [['web'], ['db'], ['web', 1], ['web'], ['db', 1], ['db']])

Output: [1, 1, None, 1, None, 1]

Hints

Hint 1

Each category is an independent problem, so keep separate state for each one in a dictionary.

Hint 2

Within one category, every label below some counter has been handed out at least once. Free labels are either the 'holes' below that counter or the counter itself and everything above it.

Hint 3

Store the holes in a min-heap. On allocate, pop from the heap if it is non-empty, otherwise use the counter and increment it. On release, push the label onto the heap.