Fair Random Sampling From a Changing Pool of Idle Drivers

Last updated: November 29, 2026

Quick Overview

Design a set of idle driver IDs supporting add(id), remove(id) and sample() in average O(1), where sample() returns a uniformly random current member, for example to pick drivers for a quality survey.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Design a set of idle driver IDs supporting add(id), remove(id) and sample() in average O(1), where sample() returns a uniformly random current member, for example to pick drivers for a quality survey.

An Uber technical screen data structure question. It checks whether you can combine an array with a hash map of indices and remove in O(1) by swapping with the last element.

What the Interviewer Expects
  • Stores members in a list for O(1) random access
  • Maps each ID to its index in the list
  • Removes by swapping with the last element and popping
  • Updates the moved element's index
  • Returns False or raises clearly for duplicate adds and missing removes
Key Topics to Cover
design a data structure
hash map
randomization
swap-remove
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support duplicates and sample proportional to multiplicity.
  • Sample weighted by hours online instead of uniformly.
  • How would you sample uniformly from a stream too large to store?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • add(id): True if newly added, False if already present.
  • remove(id): True if removed, False if absent.
  • sample(): uniformly random member; error if empty....
High-Level Design

A list gives uniform sampling via a random index, and a dict from ID to index gives O(1) membership. Removal from the middle of a list is O(n), so instead copy the last element into the removed slot, ...


Submit Your Answer
Markdown supported