← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Practice/Meta/Leetcode 398. Random Pick Index
CodingMust
Design a class that stores an integer array (which may contain duplicate values) and provides a method to retrieve a random index where a specific target value appears. When the target value exists at multiple positions in the array, each valid index must have an equal probability of being selected.
Your class should implement:
pick(target) method that returns a uniformly random index i where nums[i] == targetpick(target) method must return a valid index where the target value existspick()Example 1:
` Input: nums = [1, 2, 3, 3, 3] Operations: pick(3) → could return 2, 3, or 4 (each with ~33% probability) pick(1) → returns 0 pick(3) → could return 2, 3, or 4 (each with ~33% probability)
Explanation:
Example 2:
` Input: nums = [4, 4, 4, 4] Operations: pick(4) → could return 0, 1, 2, or 3 (each with 25% probability)
Explanation: