Design Insert/Delete/GetRandom with Weighted Sampling
Last updated: July 2, 2026
Quick Overview
Design a data structure that supports insert, delete, and getRandom operations, with the added requirement that getRandom should return elements based on their weights.
Hudson River Trading
July 2, 202613
0
113 solved
Design a data structure that supports insert, delete, and getRandom operations, with the added requirement that getRandom should return elements based on their weights.
This question is often asked in technical screening rounds to assess a candidate's understanding of data structures and algorithms. The interviewer is evaluating the candidate's ability to design efficient solutions that balance complexity with performance, particularly in scenarios where random access and weighted sampling are required. This question also tests the candidate's problem-solving skills and creativity in managing data efficiently.
What the Interviewer Expects
- Clearly define the problem and requirements.
- Explain the choice of data structures used for efficient operations.
- Discuss the time complexity of each operation.
- Provide a detailed implementation of the solution.
- Consider edge cases and potential pitfalls.
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would your design change if the weights could change dynamically?
- What would be the impact on performance if the data structure had to support concurrent modifications?
- Can you explain how you would handle duplicate weights in your design?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
To design a data structure that supports the following operations:
- Insert(value, weight): Insert a new element with an associated weight.
- Delete(value): Remove an element from the struct...
High-Level Design
We can use a combination of a list and a dictionary to implement this data structure effectively. The list will store the elements, while the dictionary will map each element to its index in the list ...