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
Software Engineering Fundamentals
Software Engineer
Hudson River Trading
July 2, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

13

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
Data Structures
Random Sampling
Time Complexity
Algorithm Design
Weighted Sampling
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements

To design a data structure that supports the following operations:

  1. Insert(value, weight): Insert a new element with an associated weight.
  2. 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 ...


Submit Your Answer
Markdown supported

Related Questions