Medium · System Design

Random Pick Index

Given an integer array nums that may contain duplicates, answer repeated pick(target) calls: return an index i with nums[i] == target, chosen uniformly at random among all such indices. The target always occurs in nums.

Examples

Example 1

[1,2,3,3,3] target=3 draws=[0,0,1]

Output: index 3

Example 2

[4,9,4,4] target=4 draws=[0,1,0]

Output: index 3

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More System Design problems