Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Design a Shuffle Function

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

Your question is Design a Shuffle Function. 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

Design a shuffle function (e.g., for a deck of cards)

Asked in the onsite_round_1 stage. The candidate noted they had seen a similar question at Amazon, received hints from the interviewer, and felt they answered poorly but got through.

Implement shuffle(items, random_values). Modify items in place and return it. The list contains arbitrary values. For deterministic testing, random_values contains n - 1 values in [0, 1), consumed while iterating from the final index down to index 1. At index i, choose j = int(random_values[k] * (i + 1)) and swap items[i] with items[j].

Constraints

  • 0 <= len(items) <= 1000
  • len(random_values) == max(0, len(items) - 1)
  • Each random value is in [0, 1)
  • The list may contain duplicate or arbitrary values

Function Signature

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