1 / 9

Perfect Shuffle

Was this Content Useful? Like Share Comment CodeGround Online Testing Platform is an online assessment and evaluation system that helps Recruiters conduct online screening tests to filter candidates before the interview process. CodeGround Recruitment Tests can be used during Campus Recruitment or screening walk-in candidates. CodeGround supports Aptitude Tests, English Communication Skills Assessments and Online Coding Contests in C, C , Java, PHP, Ruby, Python and JavaScript. CodeGround also supports asynchronous automated interviews.

Codeground
Download Presentation

Perfect Shuffle

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TECHNICAL INTERVIEW QUESTION on DATA STRUCTURES & ALGORITHMSPerfect unbiased shuffle algorithm CodeGround Online Testing Platform is an online assessment and evaluation system that helps Recruiters conduct online screening tests to filter candidates before the interview process. CodeGround Recruitment Tests can be used during Campus Recruitment or screening walk-in candidates. CodeGround supports Aptitude Tests, English Communication Skills Assessments and Online Coding Contests in C, C++, Java, PHP, Ruby, Python and JavaScript. CodeGround also supports asynchronous automated interviews.

  2. What is an unbiased shuffle algorithm? Consider an array with distinct elements A[1 … n] A perfectly unbiased shuffle algorithm would randomly shuffle all elements in the array such that after shuffling: • The probability that the shuffling operation would produce any particular permutation of the original array is the same for all permutations (i.e.) since there are n! permutations, the probability that the shuffle operation would produce any particular permutation is 1/n! • For any element e in the array and for any position j (1<= j <= n), the probability that the element would end up in position A[j] is 1/n

  3. Consider two shuffling algorithms. Which produces a perfect unbiased shuffle? SHUFFLE 1 shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between 1 and n inclusive int rand= RANDOM(1,n); swap A[i] with A[rand]; } } SHUFFLE 2 shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between i and n inclusive int rand= RANDOM(i,n); swap A[i] with A[rand]; } }

  4. shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between 1 and n inclusive int rand= RANDOM(1,n); swap A[i] with A[rand]; } } Simulation of SHUFFLE 1 rand = 3 rand = 1 rand = 2 {1,2,3} {3,2,1} {2,1,3} {1,2,3} rand = 1 rand = 1 rand = 1 {2,1,3} {2,3,1} {1,2,3} rand = 1 rand = 1 rand = 1 {3,1,2} {1,3,2} {3,2,1} rand = 2 rand = 2 rand = 2 {2,3,1} {2,1,3} {1,3,2} rand = 3 rand = 3 rand = 3 {2,1,3} {2,3,1} {1,2,3} rand = 2 rand = 2 rand = 2 {1,2,3} {3,2,1} {2,1,3} rand = 1 rand = 1 rand = 1 {3,2,1} {1,2,3} {3,1,2} rand = 2 rand = 2 rand = 2 {1,3,2} {3,1,2} {2,3,1} rand = 3 rand = 3 rand = 3 {1,2,3} {3,2,1} {2,1,3} rand = 3 rand = 3 rand = 3 {1,3,2} {3,1,2} {2,3,1} rand = 1 rand = 1 rand = 1 {2,3,1} {2,1,3} {1,3,2} rand = 2 rand = 2 rand = 2 {1,2,3} {3,2,1} {2,1,3} rand = 3 rand = 3 rand = 3 {1,3,2} {3,1,2} {2,3,1}

  5. shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between 1 and n inclusive int rand= RANDOM(1,n); swap A[i] with A[rand]; } } Simulation of SHUFFLE 1 Count of permutations: {1,2,3} – count 4 {1,3,2} – count 5 {2,1,3} – count 5 {2,3,1} – count 5 {3,1,2} – count 4 {3,2,1} – count 4

  6. shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between i and n inclusive int rand= RANDOM(i,n); swap A[i] with A[rand]; } } Simulation of SHUFFLE 2 rand = 3 rand = 1 rand = 2 {1,2,3} {3,2,1} {2,1,3} {1,2,3} rand = 2 rand = 2 rand = 2 {1,2,3} {3,2,1} {2,1,3} rand = 3 rand = 3 rand = 3 {1,2,3} {3,2,1} {2,1,3} rand = 3 rand = 3 rand = 3 {1,3,2} {3,1,2} {2,3,1} rand = 3 rand = 3 rand = 3 {1,3,2} {3,1,2} {2,3,1}

  7. shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between i and n inclusive int rand= RANDOM(i,n); swap A[i] with A[rand]; } } Simulation of SHUFFLE 2 Count of permutations: {1,2,3} – count 1 {1,3,2} – count 1 {2,1,3} – count 1 {2,3,1} – count 1 {3,1,2} – count 1 {3,2,1} – count 1

  8. Theoretical Proof that SHUFFLE 2 (Fisher-Yates shuffle) produces a perfect unbiased shuffle FISHER-YATES SHUFFLE shuffle(A[1 … n]) { for i = 1 to n { // Find a random integer between i and n inclusive int rand= RANDOM(i,n); swap A[i] with A[rand]; } } For any element e, the probability that it will be shuffled into the first position = probability that it is selected for swapping when i = 1 = 1/n For any element e, the probability that it will be shuffled into the second position = probability that it is NOT selected for the first position x probability that it is selected for swapping when i = 2 = (n-1)/n x 1/(n-1) = 1/n … For any element e, the probability that it will be shuffled into any particular position = 1/n

  9. Was this Content Useful? Comment Share Like CodeGround Online Testing Platform is an online assessment and evaluation system that helps Recruiters conduct online screening tests to filter candidates before the interview process. CodeGround Recruitment Tests can be used during Campus Recruitment or screening walk-in candidates. CodeGround supports Aptitude Tests, English Communication Skills Assessments and Online Coding Contests in C, C++, Java, PHP, Ruby, Python and JavaScript. CodeGround also supports asynchronous automated interviews.

More Related