Pergunta de entrevista da empresa Amazon

Describe data structure for Shuffle operation in a music player

Respostas da entrevista

Sigiloso

3 de jan. de 2012

There are 2 operations that the expected data structure should facilitate: 1) Randomly select an not-played song. 2) Determine if a song was played or not. I think we might to make 2 separated list, one contains the songs that are already played and the other contains the songs that are not played yet. Then using the random generator to pick one song from the unplayed list and move it to the played list once it is played. Any other better solution?

Sigiloso

6 de fev. de 2012

Implementation in java 1) Select a random number Math.random(). It returns a number between 0 and 1 and it is a a double value. 2) Muliply that number with 1000 . It returns a integer value. 3) Suppose x is length of array that contain songs, divide step2 by x. 4) the resultant value is the song to be played. 5) You can keep a counter and increment the counter if the song is played( iTunes). double d= Math.random(); int v=(int)d*1000; int x=songArray.length(); int nextSong = v % x // This value is always less than length of array. player.play(songArray(nextSong));