Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Random Number From Array

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Random Number From Array. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Upstox may need to sample values from an in-memory market-data array for simulations or testing. Implement a function that performs repeated independent random picks, where every array position has the same probability of being selected.

Duplicate values represent separate positions. For example, in [5, 5, 9], the value 5 must have probability 2/3, while 9 has probability 1/3.

Formal Specification

Implement random_picks(nums, calls, seed). The function receives a non-empty integer array nums, the number of picks to perform, and an optional integer seed used to make tests reproducible. Return an array containing calls selected values. Each pick must be independent, and the input array must not be modified.

Use Python's random.Random(seed) instance rather than the module-level generator so that the function is deterministic for a supplied seed and does not alter global random state.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= calls <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • seed is an integer
  • Do not modify nums

Function Signature

def random_picks(nums, calls, seed):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output