Random Pick Index
The goal here is to implement the Solution
class that initializes with an integer array nums
and has a method pick
, which returns a random index of a given target number from the array.
Here’s the Python implementation:


 In the constructor, we build a dictionary (
index_map
) that maps each unique number in the array to the list of its indexes in the array.  In the
pick
method, we retrieve the list of indexes for the given target and randomly pick one usingrandom.choice
.
This implementation ensures that if there are multiple valid indexes for the given target, each index will have an equal probability of being returned.