Implement a thread-safe key generation service that dispenses unique base-62 keys

Quick Overview

Implement a thread-safe key generation service that pre-generates fixed-length base-62 keys, dispenses each key once, and accepts released keys back into its pool. It tests collision-free key generation, data structures for constant-time operations, locking under concurrent access, and handling an exhausted key space.

Implement a thread-safe key generation service that dispenses unique base-62 keys

Company: Intuit

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design and implement a key generation service (KGS) that pre-generates unique, short alphanumeric keys. Systems such as URL shorteners and transaction ID generators use a service like this when they need to hand out unique short keys quickly. The service is created with a `keyLength` (the length of every key) and a `poolSize` (how many keys to pre-generate). It keeps two internal collections: an available pool of keys ready to be dispensed, and a used set of keys that have been dispensed and not yet released. Keys are generated deterministically with base-62 encoding over the characters `a-z`, `A-Z`, `0-9`. Once `getKey` dispenses a key, the service must not dispense it again unless it is explicitly returned through `releaseKey`. The service must be safe under concurrent access and must never produce a collision. Implement these methods: - `getKey() -> str` dispenses a unique key from the available pool. It returns an empty string if no keys are available. - `releaseKey(key: str) -> bool` returns a previously dispensed key to the available pool. It returns `True` if the key was released and `False` if the key was not in use. - `generateKeys(count: int) -> int` generates additional unique keys and adds them to the available pool. It returns the number of keys actually generated, which may be less than `count` if the key space is exhausted. - `availableKeys() -> int` returns the number of keys currently in the pool. - `usedKeys() -> int` returns the number of keys currently in use (dispensed and not released). - `totalKeys() -> int` returns the total number of keys the service manages (available plus used). - `isKeyUsed(key: str) -> bool` reports whether a specific key is currently in use. The two walkthroughs below treat `a` to `z`, then `A` to `Z`, then `0` to `9` as the base-62 digits 0 to 61, encode a counter that starts at 0, and dispense the most recently added key first. Any dispensing order is acceptable as long as the rules above hold. ```text service = KeyGenerationService(keyLength=2, poolSize=3) # pool holds "aa", "ab", "ac" service.availableKeys() -> 3 k = service.getKey() -> "ac" (any of the three would be valid) service.isKeyUsed(k) -> True service.usedKeys() -> 1 service.availableKeys() -> 2 service.releaseKey("zz") -> False ("zz" was never dispensed) service.releaseKey(k) -> True service.releaseKey(k) -> False (k is already back in the pool) service.generateKeys(2) -> 2 (adds "ad" and "ae") service.totalKeys() -> 5 ``` ```text service = KeyGenerationService(keyLength=1, poolSize=60) # 60 of the 62 one-character keys service.generateKeys(5) -> 2 (only "8" and "9" are left) service.totalKeys() -> 62 service.generateKeys(1) -> 0 ``` ```hint Where uniqueness comes from Look for a source of new keys that makes a repeat impossible by construction, so you never have to compare a new key against everything issued before. ``` ```hint Moves between two collections `getKey` and `releaseKey` each touch two collections. Think about what `totalKeys`, or a second `releaseKey` call for the same key, could observe halfway through one of those moves. ``` ### Constraints & Assumptions - `1 <= keyLength <= 8`, so there are `62^keyLength` possible keys: 62 when `keyLength` is 1 and about 218 trillion when it is 8. - `1 <= poolSize <= 10000` - Keys contain only `a-z`, `A-Z` and `0-9`, which gives 62 choices per position. - `getKey` returns `""` when no keys are available, and `releaseKey` returns `False` for a key that is not currently in use. - At most 10,000 calls in total across all methods. ### Clarifying Questions to Ask - When a key is released, should it be the next one handed out, or should it wait behind keys that were never used? - Is it acceptable for a key to be predictable from the previous one, or must keys look random to outside callers? - Does the service run as one process with many threads, or do several servers share the same key space? - Must the pool and the used set survive a process restart? - What should `generateKeys` do with a count of zero or a negative count? ### What a Strong Answer Covers - How uniqueness is guaranteed across generation, dispensing and release, including after the key space runs out. - The data structures behind each method and the time complexity of each operation. - Which operations hold a lock and why read-only counts need a consistent view too. - Edge cases: an empty pool, releasing an unknown or already released key, an exhausted key space and a non-positive count. - The order in which released keys are reused and what that order means for callers. ### Follow-up Questions - How would you make keys hard to guess while keeping them collision-free? - How would you run the service on several application servers without two servers handing out the same key? - What happens to pre-generated and dispensed keys if the process crashes, and what would you persist? - If `getKey` became a hot path under heavy concurrency, how would you reduce lock contention?

Overview: Implement a thread-safe key generation service that pre-generates fixed-length base-62 keys, dispenses each key once, and accepts released keys back into its pool. It tests collision-free key generation, data structures for constant-time operations, locking under concurrent access, and handling an exhausted key space.

Intuit logo
Intuit
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a key generation service (KGS) that pre-generates unique, short alphanumeric keys. Systems such as URL shorteners and transaction ID generators use a service like this when they need to hand out unique short keys quickly.

The service is created with a keyLength (the length of every key) and a poolSize (how many keys to pre-generate). It keeps two internal collections: an available pool of keys ready to be dispensed, and a used set of keys that have been dispensed and not yet released.

Keys are generated deterministically with base-62 encoding over the characters a-z, A-Z, 0-9. Once getKey dispenses a key, the service must not dispense it again unless it is explicitly returned through releaseKey. The service must be safe under concurrent access and must never produce a collision.

Implement these methods:

  • getKey() -> str dispenses a unique key from the available pool. It returns an empty string if no keys are available.
  • releaseKey(key: str) -> bool returns a previously dispensed key to the available pool. It returns True if the key was released and False if the key was not in use.
  • generateKeys(count: int) -> int generates additional unique keys and adds them to the available pool. It returns the number of keys actually generated, which may be less than count if the key space is exhausted.
  • availableKeys() -> int returns the number of keys currently in the pool.
  • usedKeys() -> int returns the number of keys currently in use (dispensed and not released).
  • totalKeys() -> int returns the total number of keys the service manages (available plus used).
  • isKeyUsed(key: str) -> bool reports whether a specific key is currently in use.

The two walkthroughs below treat a to z, then A to Z, then 0 to 9 as the base-62 digits 0 to 61, encode a counter that starts at 0, and dispense the most recently added key first. Any dispensing order is acceptable as long as the rules above hold.

service = KeyGenerationService(keyLength=2, poolSize=3)   # pool holds "aa", "ab", "ac"
service.availableKeys()   -> 3
k = service.getKey()      -> "ac"    (any of the three would be valid)
service.isKeyUsed(k)      -> True
service.usedKeys()        -> 1
service.availableKeys()   -> 2
service.releaseKey("zz")  -> False   ("zz" was never dispensed)
service.releaseKey(k)     -> True
service.releaseKey(k)     -> False   (k is already back in the pool)
service.generateKeys(2)   -> 2       (adds "ad" and "ae")
service.totalKeys()       -> 5
service = KeyGenerationService(keyLength=1, poolSize=60)  # 60 of the 62 one-character keys
service.generateKeys(5)   -> 2       (only "8" and "9" are left)
service.totalKeys()       -> 62
service.generateKeys(1)   -> 0

Constraints & Assumptions

  • 1 <= keyLength <= 8 , so there are 62^keyLength possible keys: 62 when keyLength is 1 and about 218 trillion when it is 8.
  • 1 <= poolSize <= 10000
  • Keys contain only a-z , A-Z and 0-9 , which gives 62 choices per position.
  • getKey returns "" when no keys are available, and releaseKey returns False for a key that is not currently in use.
  • At most 10,000 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • When a key is released, should it be the next one handed out, or should it wait behind keys that were never used?
  • Is it acceptable for a key to be predictable from the previous one, or must keys look random to outside callers?
  • Does the service run as one process with many threads, or do several servers share the same key space?
  • Must the pool and the used set survive a process restart?
  • What should generateKeys do with a count of zero or a negative count?

What a Strong Answer Covers Guidance

  • How uniqueness is guaranteed across generation, dispensing and release, including after the key space runs out.
  • The data structures behind each method and the time complexity of each operation.
  • Which operations hold a lock and why read-only counts need a consistent view too.
  • Edge cases: an empty pool, releasing an unknown or already released key, an exhausted key space and a non-positive count.
  • The order in which released keys are reused and what that order means for callers.

Follow-up Questions Guidance

  • How would you make keys hard to guess while keeping them collision-free?
  • How would you run the service on several application servers without two servers handing out the same key?
  • What happens to pre-generated and dispensed keys if the process crashes, and what would you persist?
  • If getKey became a hot path under heavy concurrency, how would you reduce lock contention?