Design Insert/Delete/GetRandom with Weighted Sampling

Quick Overview

This Hudson River Trading software engineering question combines insert, delete, random access, and weighted sampling in one data structure design. It is useful preparation for interviews that probe invariants, expected complexity, and how to maintain auxiliary state correctly.

Design Insert/Delete/GetRandom with Weighted Sampling

Company: Hudson River Trading

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Design a data structure that supports inserting values, deleting values, and returning a random value. Then extend it so each inserted value has a weight and `getRandom` returns values with probability proportional to weight. ### Constraints & Assumptions - Values are unique unless clarified. - Unweighted operations should be O(1) average time. - Weighted sampling can be O(log n) unless the interviewer requires O(1). - Weights are positive numbers. ### Clarifying Questions to Ask - Can duplicate values be inserted? - Do weights change after insertion? - Are random calls much more frequent than updates? - Is exact weighted probability required? - What language and random library are available? ### What a Strong Answer Covers ```premium-lock What a Strong Answer Covers ``` ### Follow-up Questions - How would you support duplicate values? - How would you update a weight? - How would you test random distribution? - When would alias tables be better than Fenwick trees?

Quick Answer: This Hudson River Trading software engineering question combines insert, delete, random access, and weighted sampling in one data structure design. It is useful preparation for interviews that probe invariants, expected complexity, and how to maintain auxiliary state correctly.

|Home/Software Engineering Fundamentals/Hudson River Trading
Hudson River Trading logo
Hudson River Trading
Jul 2, 2026, 7:02 PM
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
10
0

Design a data structure that supports inserting values, deleting values, and returning a random value. Then extend it so each inserted value has a weight and getRandom returns values with probability proportional to weight.

Constraints & Assumptions

  • Values are unique unless clarified.
  • Unweighted operations should be O(1) average time.
  • Weighted sampling can be O(log n) unless the interviewer requires O(1).
  • Weights are positive numbers.

Clarifying Questions to Ask Guidance

  • Can duplicate values be inserted?
  • Do weights change after insertion?
  • Are random calls much more frequent than updates?
  • Is exact weighted probability required?
  • What language and random library are available?

What a Strong Answer Covers Premium

Follow-up Questions Guidance

  • How would you support duplicate values?
  • How would you update a weight?
  • How would you test random distribution?
  • When would alias tables be better than Fenwick trees?
Loading comments...