Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Card Shuffling Program

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

Your question is Card Shuffling Program. 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

Applied Epic workflows may need to randomize a set of cards used in training or simulation exercises. Implement a function that produces a uniformly shuffled copy of a deck using the Fisher-Yates algorithm.

For deterministic evaluation, the function receives random_values. During the shuffle, each value supplies the selected swap index for one iteration. The first value is used when i = n - 1, the second when i = n - 2, and so on. Every supplied value must already satisfy 0 <= random_values[k] <= i for its iteration.

The original deck must remain unchanged. Return a new list containing exactly the same cards, each appearing once.

Formal Specification

  • Input: deck, a list of distinct strings, and random_values, a list of integers with length max(0, len(deck) - 1).
  • Output: A new list containing a permutation of deck.
  • Do not call Python's built-in shuffle function.

Constraints

  • 0 <= len(deck) <= 10^5
  • All card labels are distinct strings
  • len(random_values) = max(0, len(deck) - 1)
  • For iteration i, the corresponding random value satisfies 0 <= random_value <= i

Function Signature

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