Skip to content
Problem 710 · LeetCode 710(opens in a new tab)

Random Pick with Blacklist

HardHash tables

Design a class built from a whole number n and an array blacklist of distinct numbers from the range 0 to n − 1. It has one method, pick(), which returns a random number from 0 to n − 1 that is not on the blacklist.

Every allowed number must come up with the same chance. Try to call the language's random function as few times as you can; ideally once per pick.

Example 1

Input:  Solution(8, [0, 3, 6]), then pick() twice
Output: e.g. 7, then 2
Why:    1, 2, 4, 5 and 7 are allowed; each has chance 1/5
2 more examples and the constraints

Example 2

Input:  Solution(3, []), then pick()
Output: 0, 1 or 2
Why:    nothing is banned, so all three are equally likely

Example 3

Input:  Solution(2, [0]), then pick()
Output: 1
Why:    1 is the only allowed number

Constraints

  • 1 ≤ n ≤ 1,000,000,000
  • 0 ≤ length of blacklist ≤ min(100,000, n − 1), so at least one number is allowed
  • 0 ≤ blacklist[i] < n, and all values are distinct
  • pick() is called at most 20,000 times

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Draw and retryO(n / (n − b)) per pick
  2. Remap banned numbersO(1) per pick

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRandom calls for 20,000 picks with 100,000 of 100,001 bannedVerdict
Draw and retryO(n / (n − b)) per pick
Fine when few are banned
Remap banned numbersO(1) per pick
Best; one draw per pick

n = size of the range, b = length of the blacklist.

Edge cases to test

  • Empty blacklist. k = n and the map is empty; pick is a plain uniform draw.
  • Only one allowed number. Solution(2, [0]) must return 1 every time.
  • All banned numbers at the top. With n = 10 and [7, 8, 9], k = 7 and nothing needs remapping.
  • All banned numbers at the bottom. With n = 10 and [0, 1, 2, 3], each of them maps to 6, 7, 8 or 9.
  • Huge n. n can be 1,000,000,000, so never build a list of all allowed numbers.

Common mistakes

  • Building a list of every allowed number, which can need a billion entries.
  • Choosing a spare without skipping numbers that are banned themselves, so pick can return a banned number.
  • Drawing from 0 to n − 1 in the remap version, which makes numbers at or above k come up twice as often.

How to talk through it

  1. 1Clarify. How large can n be compared with the blacklist? Is using few random calls a hard requirement?
  2. 2Give draw and retry. Uniform and simple, but it can need many draws when almost everything is banned.
  3. 3Shrink the range. Draw from 0 to k − 1, where k counts the allowed numbers.
  4. 4Patch the holes. Map each banned number below k to a free number at or above k, once, in the constructor.
  5. 5Test out loud. Run an empty blacklist, one allowed number, and banned numbers that are also spares.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?