Prepin
Log in

Create a module that retrieves a unique User ID (integer) from a set of available User IDs. Afterward, return the ID back to the pool for future utilization.

Approach / Explanation

This question requires you to design a module or system that is primarily responsible for overseeing a collection of unique User IDs stored as integers. The module should be capable of retrieving a unique User ID from this collection upon request and subsequently allowing the User ID to be returned to the collection for future use. To provide a comprehensive answer: Outline your approach to managing the collection of User IDs. Detail the process for obtaining a unique ID from the collection. Clarify how you would reintegrate the ID back into the collection.

Suggested Answer

A possible approach to handle a collection of User IDs is by utilizing a data structure such as a set in memory. In various programming languages, a Set does not permit duplicates, thereby ensuring that each User ID is unique. When a new User ID is requested, we can take one ID from the set and return it. To return a User ID back to the pool, we simply add it back into the set. Below is an example in pseudo code: class UserIDPool : def __init__ ( self ) : self . ID_set = set ( range ( 1 , 10001 ) ) # Assuming we have 10000 User IDs def fetch_ID ( self ) : return self . ID_set . pop ( ) def return_ID ( self , ID ) : self . ID_set . add ( ID )

Alternative Answer

One alternative for managing a pool of User IDs is to utilize a database with two tables: one for available User IDs and another for those currently in use. When a new User ID is requested, we can perform a database transaction to transfer an ID from the first table to the second, ensuring that the ID cannot be accessed by another process until the transaction is fully committed. To return an ID, we would execute a similar transaction to move it back from the used ID table to the available ID table. This approach would be more robust and scalable compared to an in-memory set, but it would also introduce more complexity and could be slower due to the database operations involved. Below is a pseudo SQL example: START TRANSACTION ; INSERT INTO Used_IDs SELECT ID FROM Available_IDs LIMIT 1 ; DELETE FROM Available_IDs WHERE ID = ( SELECT ID FROM Used_IDs ORDER BY ID DESC LIMIT 1 ) ; COMMIT ; Returning the ID would involve a similar set of transactions, but in reverse order.

Question Details

Difficulty & Category

Medium
technical

Companies

Dropbox

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.