Perfect Shuffle - PowerPoint PPT Presentation

About This Presentation
Title:

Perfect Shuffle

Description:

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. – PowerPoint PPT presentation

Number of Views:41
Slides: 10
Provided by: codeground

less

Transcript and Presenter's Notes

Title: Perfect Shuffle


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 A1 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 (1lt j lt n), the probability that the
    element would end up in position Aj is 1/n

3
Consider two shuffling algorithms. Which produces
a perfect unbiased shuffle?
SHUFFLE 2 shuffle(A1 n) for i 1 to
n // Find a random integer between i
and n inclusive int rand RANDOM(i,n)
swap Ai with Arand
SHUFFLE 1 shuffle(A1 n) for i 1 to
n // Find a random integer between 1
and n inclusive int rand RANDOM(1,n)
swap Ai with Arand
4
Simulation of SHUFFLE 1
shuffle(A1 n) for i 1 to n
// Find a random integer between 1 and n
inclusive int rand RANDOM(1,n)
swap Ai with Arand
  • 1,2,3

rand 3
rand 1
rand 2
1,2,3
2,1,3
3,2,1
rand 1
rand 1
rand 1
2,1,3
1,2,3
2,3,1
rand 1
rand 1
rand 1
3,1,2
3,2,1
1,3,2
rand 2
rand 2
rand 2
2,3,1
1,3,2
2,1,3
rand 3
rand 3
rand 3
2,1,3
1,2,3
2,3,1
rand 2
rand 2
rand 2
1,2,3
2,1,3
3,2,1
rand 1
rand 1
rand 1
3,2,1
3,1,2
1,2,3
rand 2
rand 2
rand 2
1,3,2
3,1,2
2,3,1
rand 3
rand 3
rand 3
1,2,3
2,1,3
3,2,1
rand 3
rand 3
rand 3
1,3,2
2,3,1
3,1,2
rand 1
rand 1
rand 1
2,3,1
1,3,2
2,1,3
rand 2
rand 2
rand 2
1,2,3
2,1,3
3,2,1
rand 3
rand 3
rand 3
1,3,2
2,3,1
3,1,2
5
Simulation of SHUFFLE 1
shuffle(A1 n) for i 1 to n
// Find a random integer between 1 and n
inclusive int rand RANDOM(1,n)
swap Ai with Arand
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
Simulation of SHUFFLE 2
shuffle(A1 n) for i 1 to n
// Find a random integer between i and n
inclusive int rand RANDOM(i,n)
swap Ai with Arand
  • 1,2,3

rand 3
rand 1
rand 2
1,2,3
2,1,3
3,2,1
rand 2
rand 2
rand 2
1,2,3
2,1,3
3,2,1
rand 3
rand 3
rand 3
1,2,3
2,1,3
3,2,1
rand 3
rand 3
rand 3
1,3,2
2,3,1
3,1,2
rand 3
rand 3
rand 3
1,3,2
2,3,1
3,1,2
7
Simulation of SHUFFLE 2
shuffle(A1 n) for i 1 to n
// Find a random integer between i and n
inclusive int rand RANDOM(i,n)
swap Ai with Arand
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(A1 n)
for i 1 to n // Find a random
integer between i and n inclusive int
rand RANDOM(i,n) swap Ai with
Arand
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?
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.
Write a Comment
User Comments (0)
About PowerShow.com