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.
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Perfect shuffle code ground
1. TECHNICAL INTERVIEW QUESTION on DATA
STRUCTURES & ALGORITHMS
Perfect 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:
1. 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!
2. 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 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];
}
}
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];
}
}
5. Simulation of 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];
}
}
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
{1,2,3}
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];
}
}
{1,2,3}rand = 1
{1,2,3}
{1,2,3}rand = 3
{1,3,2}
{1,3,2}rand = 3
rand = 2
rand = 3
{2,1,3}rand = 2
{2,1,3}
{2,1,3}rand = 3
{2,3,1}
{2,3,1}rand = 3
rand = 2
rand = 3
{3,2,1}
{3,2,1}
{3,2,1}rand = 3
{3,1,2}
{3,1,2}rand = 3
rand = 2
rand = 3
rand = 3
7. Simulation of 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];
}
}
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?
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.