Random Pick Index - Problem
Random Pick Index is a fascinating problem that combines data structures with probability theory. You're given an integer array nums that may contain duplicate values, and your task is to implement a class that can randomly select an index for any given target number.

The challenge lies in ensuring uniform probability - if a target appears multiple times in the array, each occurrence should have an equal chance of being selected. For example, if the number 7 appears at indices [2, 5, 9], then each of these indices should have a 1/3 probability of being returned.

Your mission: Design a Solution class with two methods:
โ€ข Solution(int[] nums) - Initialize with the input array
โ€ข int pick(int target) - Return a random index where nums[index] == target

Input & Output

example_1.py โ€” Basic Usage
$ Input: nums = [1,2,3,3,3], target = 3
โ€บ Output: 2 or 3 or 4 (each with 1/3 probability)
๐Ÿ’ก Note: The target 3 appears at indices [2,3,4]. Each index should have equal probability of being returned, so valid outputs are 2, 3, or 4.
example_2.py โ€” Single Occurrence
$ Input: nums = [1,2,3,3,3], target = 2
โ€บ Output: 1
๐Ÿ’ก Note: The target 2 appears only at index 1, so we always return 1.
example_3.py โ€” Multiple Calls
$ Input: nums = [1,2,3,3,3], multiple pick(3) calls
โ€บ Output: Various combinations of [2,3,4]
๐Ÿ’ก Note: Over many calls, indices 2, 3, and 4 should each be returned approximately 1/3 of the time, demonstrating uniform random distribution.

Visualization

Tap to expand
Random Pick Index: Strategy Comparison๐Ÿ”จ Brute ForceTime: O(n) per pickSpace: O(1)Scan array every timeSimple but inefficient๐Ÿ“š Hash TableTime: O(1) per pickSpace: O(n)Preprocess onceFast queriesโšก ReservoirTime: O(n) per pickSpace: O(1)Mathematical eleganceOptimal spaceProbability Distribution Guarantee:All three approaches ensure each valid index has equal probability:โ€ข For target appearing n times: P(each index) = 1/nโ€ข Uniform random distribution maintainedโ€ข No bias toward any particular occurrenceChoose Based On:โ€ข Memory constraints โ†’ Reservoir Samplingโ€ข Query frequency โ†’ Hash Table (if many picks)โ€ข Simplicity โ†’ Brute Force (if few picks)โ˜…
Understanding the Visualization
1
Brute Force
Scan entire array each time - Simple but slow O(n) per pick
2
Hash Preprocessing
Build lookup table once - Fast O(1) picks but uses O(n) space
3
Reservoir Sampling
Mathematical elegance - O(1) space with uniform probability guarantee
Key Takeaway
๐ŸŽฏ Key Insight: Reservoir sampling achieves the seemingly impossible - uniform random selection from an unknown-size stream using only O(1) space through clever probability mathematics!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each pick() requires scanning the entire array once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only uses a constant amount of extra variables regardless of input size

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • -231 โ‰ค nums[i] โ‰ค 231 - 1
  • target is guaranteed to exist in nums
  • At most 104 calls will be made to pick
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen