Shuffle a Deck From Random Choices
Updated
Problem statement
Implement the Fisher-Yates shuffle for an integer array deck. The input also includes picks, the random choices to use, so the result can be tested exactly.
Process i from deck.length - 1 down to 1. At that step, use the next value j from picks, where 0 <= j <= i, and swap deck[i] with deck[j]. Return the shuffled deck.
In a real randomized implementation, each pick is produced independently by random.randint(0, i) or an equivalent inclusive random-integer API.
Function
shuffleDeck(deck: int[], picks: int[]) → int[]Examples
Example 1
deck = [1,2,3,4]picks = [1,0,1]return = [3,4,1,2]Swap positions (3,1), then (2,0), then (1,1).
Example 2
deck = [1,2,3]picks = [2,1]return = [1,2,3]Both choices select the current index, so no values move.
Example 3
deck = [1,2,3]picks = [0,0]return = [2,3,1]The last item moves to the front, then the remaining prefix is shuffled.
Constraints
1 <= deck.length <= 200000.picks.length = deck.length - 1.- For pick index
t,0 <= picks[t] <= deck.length - 1 - t.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Latency Bucket CounterDatadog · OA · PHONE SCREENDatadog interview questions
- Mini-Batch Data LoaderOpenAI · PHONE SCREENOpenAI interview questions
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Deterministic K-Means AssignmentsLyft · PHONE SCREENLyft interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions