Shuffle a Playlist
Updated
Problem statement
You are given the song IDs in a playlist, songs. Shuffle its entries while preserving every occurrence, including repeated song IDs.
For this exercise, assume the random choices are supplied in choices so the result can be tested exactly. Apply the forward Fisher–Yates procedure: at step i, swap playlist positions i and i + choices[i]. Process steps from 0 through songs.length - 1.
Each supplied choice is a relative offset in 0..songs.length-i-1. If these offsets are independently uniform over their stated ranges, this procedure produces a uniform random permutation of the original playlist entries.
Return a new array containing the result of these supplied choices. Do not change songs. An empty playlist returns an empty array.
Function
shufflePlaylist(songs: int[], choices: int[]) → int[]Examples
Example 1
songs = [10,20,30,40]choices = [2,0,1,0]return = [30,20,40,10]Swap positions 0 and 2, then 1 with itself, then 2 and 3. The final step is a self-swap.
Example 2
songs = [1,2,3]choices = [1,1,0]return = [2,3,1]The first swap exchanges positions 0 and 1. The next exchanges positions 1 and 2.
Example 3
songs = []choices = []return = []An empty playlist has no shuffle steps.
Constraints
0 <= songs.length <= 1000, andchoices.length = songs.length.0 <= songs[i] <= 10^9. Repeated IDs represent separate playlist entries.0 <= choices[i] < songs.length - i.
More Roblox Onsite Interview questions
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Search Autocomplete SystemPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Candy Crush Grid Matching and GravityPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Most Frequent Call Stack Per ThreadPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count and Score Distinct Hat AssignmentsONSITE INTERVIEW · Seen Sep 2026
- Rate Limiter Sliding Window With Per-Entity LimitsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Most Frequent Call Path in Interleaved LogsONSITE INTERVIEW · Seen Aug 2026
- Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Maximize Distance to Closest Person — Return the SeatONSITE INTERVIEW · Seen Jun 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Copy File Bytes with Partial WritesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Policy Violation Set ChecksPinterest · ONSITE INTERVIEWPinterest interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions