Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Random Sorting Algorithm

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

Your question is Random Sorting Algorithm. 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

Intelligent Medical Objects may need to vary the display order of terminology candidates so that a fixed input order does not introduce presentation bias. Given a list of numbers representing candidate scores, return a new list containing the same values in a uniformly random order.

Use a supplied integer seed so the result is reproducible for testing. Implement the shuffle with the Fisher-Yates algorithm. Do not modify the input list.

Formal Specification

Implement shuffle_numbers(nums, seed). The parameter nums is a list of integers, and seed is an integer. Return a new list containing exactly the same elements as nums, in a permutation determined by seed.

Use Python's random.Random(seed) as the pseudorandom generator. At position i, choose the swap index uniformly from 0 through i, inclusive. The algorithm must not use random.shuffle directly.

Constraints

  • 0 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • seed is an integer
  • Duplicate values may occur
  • The input list must remain unchanged

Function Signature

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