FastPrepShuffle a Playlist

Shuffle a Playlist

Roblox logoRoblox● EasyNEW GRADONSITE INTERVIEW
Learn

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 mutate 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, and choices.length = songs.length.
  • 0 <= songs[i] <= 10^9. Repeated IDs represent separate playlist entries.
  • 0 <= choices[i] < songs.length - i.

More Roblox problems

See Roblox hiring insights
public int[] shufflePlaylist(int[] songs, int[] choices) {
    // write your code here
}
songs[10,20,30,40]
choices[2,0,1,0]
expected[30,20,40,10]
Checking account…