Skip to main content

Random Pick Index - Solution & Explanation

MediumHash TableMathReservoir SamplingRandomized10 min readAsked at: Amazon, Meta, NVIDIA +1
Practice this problem

Problem Statement

Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Implement the Solution class:

  • Solution(int[] nums) Initializes the object with the array nums.
  • int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i's, then each index should have an equal probability of returning.

 

Example 1:

Input
["Solution", "pick", "pick", "pick"]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
Output
[null, 4, 0, 2]

Explanation
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.

 

Constraints:

  • 1 <= nums.length <= 2 * 104
  • -231 <= nums[i] <= 231 - 1
  • target is an integer from nums.
  • At most 104 calls will be made to pick.

Approach Overview

Problem Overview: You receive an integer array where the same value may appear multiple times. When pick(target) is called, return a random index where the target occurs. If the target appears multiple times, every valid index must have the same probability of being chosen.

Approach 1: Simple Random Selection with Array (Preprocessing Hash Map) (Time: O(n) build, O(1) pick | Space: O(n))

Store every index of each number during initialization. Use a hash table where the key is the value and the value is a list of indices where it appears. When pick(target) is called, retrieve the index list and generate a random number within its range. Return the element at that random position. The key insight is that precomputing positions converts the selection step into constant time. This approach works well when you expect many pick calls after initialization and memory usage is not a concern.

Approach 2: Reservoir Sampling (Time: O(n) per pick | Space: O(1))

When extra memory is restricted or the array is extremely large, reservoir sampling provides a clean solution. Iterate through the array and track how many times the target has been seen. Each time the target appears, generate a random number between 1 and the current count. Replace the stored result if the random value equals 1. This guarantees that every valid index has equal probability of selection without storing all positions. The technique comes from streaming algorithms and fits problems involving randomized algorithms with unknown or large datasets.

Recommended for interviews: Reservoir Sampling is typically the expected solution because it demonstrates understanding of probability and space optimization. The hash map approach is easier to implement and often acceptable if multiple queries are expected. Showing both approaches signals strong problem-solving range: first identify the straightforward preprocessing method, then optimize space using probabilistic sampling.

Approach 1: Simple Random Selection with Array

This approach involves traversing the entire array to collect all the indices of the given target. Once we have the indices, we can randomly select one of the indices using a random number generator. This approach is straightforward but can be less efficient if the target picking operation is frequently called.

The Python solution uses a list comprehension to store all the indices of the target in the list indices. Then, it uses random.choice() to randomly pick and return one of these indices.

Code

Python

C++

Java

C#

JavaScript

Complexity

Time Complexity: O(n) for each pick operation, where n is the number of elements in nums since we traverse the array to gather indices.
Space Complexity: O(n) to store the indices of the target.

Try this approach in the editor →

Approach 2: Reservoir Sampling

Reservoir sampling is a technique to randomly choose a sample of k items from a list of n items, where n is either a very large or unknown number. It's useful in our context to allow random selection with a single pass over the data.

The reservoir sampling technique is applied here by incrementing the count each time the target is encountered. We then choose to reset result to the current index with probability 1/count.

Code

Python

C++

Java

C#

JavaScript

Complexity

Time Complexity: O(n) for each pick operation, where n is the size of the array.
Space Complexity: O(1) as no extra data structures proportional to input size are used.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Random Selection with Array

Time Complexity: O(n) for each pick operation, where n is the number of elements in nums since we traverse the array to gather indices.
Space Complexity: O(n) to store the indices of the target.

Reservoir Sampling

Time Complexity: O(n) for each pick operation, where n is the size of the array.
Space Complexity: O(1) as no extra data structures proportional to input size are used.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Random Selection with Array (Hash Map)O(n) preprocessing, O(1) per pickO(n)Best when many pick calls occur and extra memory is acceptable
Reservoir SamplingO(n) per pickO(1)When memory is constrained or the array cannot store all indices

Video Solution

RANDOM PICK INDEX | LEETCODE # 398 | PYTHON RESERVOIR SAMPLINGCracking FAANG13,935 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Random Pick Index easy or hard?
Random Pick Index is considered a medium difficulty problem. The hash map solution is straightforward, but recognizing and implementing reservoir sampling requires deeper understanding of randomized algorithms.
How to solve Random Pick Index in O(n)?
Reservoir Sampling solves the problem in O(n) time for each pick operation. Iterate through the array, count occurrences of the target, and randomly replace the stored index with probability 1/count whenever the target appears.
What is the best approach for Random Pick Index?
Reservoir Sampling is the most space‑efficient approach. It scans the array once per pick and keeps only one candidate index, ensuring each valid index has equal probability. The time complexity is O(n) per pick with O(1) extra space.
Is Random Pick Index asked at Google/Amazon/Meta?
Randomized selection and reservoir sampling problems appear in interviews at companies like Google, Amazon, and Meta because they test probability reasoning and space‑efficient algorithm design.
What data structure is used in Random Pick Index?
The straightforward solution uses a hash table mapping each number to a list of its indices. The optimized solution uses reservoir sampling, which relies on random number generation and a simple counter rather than additional data structures.
What is the time complexity of Random Pick Index?
Two common implementations exist. Using a hash map of value to index list requires O(n) preprocessing and O(1) time for each pick call. Reservoir Sampling avoids preprocessing but requires O(n) time per pick with O(1) space.
Random Pick Index Python or Java solution approach?
In Python or Java, the common implementation stores indices in a HashMap or dictionary and selects a random index using a built‑in random generator. The reservoir sampling version iterates through the array and updates the chosen index probabilistically.

Ready to solve this problem?

Practice Random Pick Index with our built-in code editor and test cases.

Practice on FleetCode