Random Pick Index

To solve this coding challenge, we need to create a class
Solution
which will handle two primary operations: initializing the object with an array of integers and picking a random index where a given target number is present in the array. The implementation needs to ensure that each eligible index is returned with equal probability.

Explanation

We will break down the solution into the following steps:
  1. Initialization :
    • We need a constructor to initialize the object with the given integer array
      nums
      . This will store the array for later use.
  2. Picking a Random Index :
    • We need to implement a method
      pick
      which will take a target number as input.
    • This method should identify all the indices in the array where the target number is present.
    • It will then randomly select one of these indices and return it.

    Detailed Steps in Pseudocode

  3. Class Initialization :
    • When the
      Solution
      class is instantiated, store the provided array in an instance variable.
  4. pick Method :
    • Initialize an empty list to store indices where the target number is found.
    • Loop through the array to find all indices that match the target number and store these indices in the list.
    • Use a random selection method to pick one of these indices uniformly at random and return it.
    Below is a detailed pseudocode with comments explaining each step:
                                                
    # Class to solve the coding challenge
    class Solution:
    # Constructor to initialize the object with the array nums
    def __init__(self, nums):
    # Store the provided array in the instance variable
    self.nums = nums
    
    # Method to pick a random index where the target number is present
    def pick(self, target):
    # Initialize an empty list to store indices of target number
    target_indices = []
    
    # Loop through each element in the array
    for index in range(len(self.nums)):
    # If the element matches the target number
    if self.nums[index] == target:
    # Add the index to the list
    target_indices.append(index)
    
    # Randomly select and return one of the indices where target was found
    return random.choice(target_indices)
    
                                            

    Step-by-Step Explanation of Pseudocode

  5. Class Definition :
    • Define a class
      Solution
      which will encapsulate our solution.
  6. Constructor (
    __init__
    )
    :
    • The constructor method
      __init__
      takes an array
      nums
      as input and stores it in an instance variable
      self.nums
      .
    • This allows us to access the array across different methods within the class.
  7. Pick Method :
    • The method
      pick
      will accept a target number as input.
    • We initialize an empty list
      target_indices
      to keep track of indices where the target number is found.
    • We loop through each element in
      self.nums
      using its index.
    • For each element, we check if the current element matches the target number.
    • If it matches, we append the current index to the
      target_indices
      list.
    • After the loop, we use
      random.choice()
      to randomly select one of the indices from
      target_indices
      and return it. This ensures that each index has an equal probability of being chosen.
By following these steps, the solution ensures that:
  • The
    Solution
    class is appropriately initialized with the provided array.
  • The
    pick
    method correctly identifies all indices of the target number and returns one of those indices at random with equal probability.