Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shuffle Deck Algorithm

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

Your question is Shuffle Deck 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 use randomized card-style test data when validating terminology workflows. Given a list representing a deck, implement an in-place shuffle so every possible ordering is equally likely.

Use the Fisher-Yates algorithm. The function must modify the input list and return it. For deterministic testing, accept an optional seed and use Python's random.Random(seed) as the random number generator. If seed is None, use nondeterministic randomness.

Formal Specification

Implement shuffle_deck(deck, seed), where deck is a mutable list of distinct or repeated values and seed is either an integer or None. Return the same list object after shuffling it in place. The output must contain exactly the same values and frequencies as the input.

With a fixed seed, the result must be reproducible. Without a fixed seed, every permutation should have equal probability.

Constraints

  • 0 <= len(deck) <= 10^5
  • The input list may contain repeated values.
  • The function must modify the list in place.
  • Use O(1) auxiliary space apart from the random generator.

Function Signature

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