SlideShare a Scribd company logo
1 of 9
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.
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
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];
}
}
Simulation of SHUFFLE 1
{1,2,3}
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];
}
}
{1,2,3}
{2,1,3}
rand = 1
rand = 1
{3,1,2}rand = 1
{2,3,1}rand = 2
{2,1,3}rand = 3
{1,2,3}
{3,2,1}rand = 1
{1,3,2}rand = 2
{1,2,3}rand = 3
{1,3,2}
{2,3,1}rand = 1
{1,2,3}rand = 2
{1,3,2}rand = 3
rand = 2
rand = 3
{2,1,3}
{1,2,3}
rand = 2
rand = 1
{3,2,1}rand = 1
{1,3,2}rand = 2
{1,2,3}rand = 3
{2,1,3}
{3,1,2}rand = 1
{2,3,1}rand = 2
{2,1,3}rand = 3
{2,3,1}
{1,3,2}rand = 1
{2,1,3}rand = 2
{2,3,1}rand = 3
rand = 2
rand = 3
{3,2,1}
{2,3,1}rand = 1
{1,3,2}rand = 1
{2,1,3}rand = 2
{2,3,1}rand = 3
{3,2,1}
{1,2,3}rand = 1
{3,1,2}rand = 2
{3,2,1}rand = 3
{3,1,2}
{2,1,3}rand = 1
{3,2,1}rand = 2
{3,1,2}rand = 3
rand = 2
rand = 3
rand = 3
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
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
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
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
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 Related Content

Recently uploaded

原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量sehgh15heh
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一A SSS
 
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一lvtagr7
 
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdf
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdfMost Inspirational Leaders Empowering the Educational Sector, 2024.pdf
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdfTheKnowledgeReview2
 
MIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewMIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewSheldon Byron
 
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxGraduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxJobs Finder Hub
 
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一fjjwgk
 
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewCrack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewNilendra Kumar
 
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...RegineManuel2
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012sapnasaifi408
 
Protection of Children in context of IHL and Counter Terrorism
Protection of Children in context of IHL and  Counter TerrorismProtection of Children in context of IHL and  Counter Terrorism
Protection of Children in context of IHL and Counter TerrorismNilendra Kumar
 
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Black and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfBlack and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfpadillaangelina0023
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一2s3dgmej
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一Fs sss
 
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docx
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docxOutsmarting the Attackers A Deep Dive into Threat Intelligence.docx
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docxmanas23pgdm157
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证nhjeo1gg
 
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一A SSS
 
办理哈珀亚当斯大学学院毕业证书文凭学位证书
办理哈珀亚当斯大学学院毕业证书文凭学位证书办理哈珀亚当斯大学学院毕业证书文凭学位证书
办理哈珀亚当斯大学学院毕业证书文凭学位证书saphesg8
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveMarharyta Nedzelska
 

Recently uploaded (20)

原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
 
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一
定制(UQ毕业证书)澳洲昆士兰大学毕业证成绩单原版一比一
 
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdf
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdfMost Inspirational Leaders Empowering the Educational Sector, 2024.pdf
Most Inspirational Leaders Empowering the Educational Sector, 2024.pdf
 
MIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewMIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx review
 
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxGraduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
 
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一
定制(ECU毕业证书)埃迪斯科文大学毕业证毕业证成绩单原版一比一
 
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewCrack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
 
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...
Drawing animals and props.pptxDrawing animals and props.pptxDrawing animals a...
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
 
Protection of Children in context of IHL and Counter Terrorism
Protection of Children in context of IHL and  Counter TerrorismProtection of Children in context of IHL and  Counter Terrorism
Protection of Children in context of IHL and Counter Terrorism
 
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
格里菲斯大学毕业证(Griffith毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Black and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdfBlack and White Minimalist Co Letter.pdf
Black and White Minimalist Co Letter.pdf
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docx
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docxOutsmarting the Attackers A Deep Dive into Threat Intelligence.docx
Outsmarting the Attackers A Deep Dive into Threat Intelligence.docx
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
 
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
 
办理哈珀亚当斯大学学院毕业证书文凭学位证书
办理哈珀亚当斯大学学院毕业证书文凭学位证书办理哈珀亚当斯大学学院毕业证书文凭学位证书
办理哈珀亚当斯大学学院毕业证书文凭学位证书
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental Leave
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
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]; } }
  • 4. Simulation of SHUFFLE 1 {1,2,3} 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]; } } {1,2,3} {2,1,3} rand = 1 rand = 1 {3,1,2}rand = 1 {2,3,1}rand = 2 {2,1,3}rand = 3 {1,2,3} {3,2,1}rand = 1 {1,3,2}rand = 2 {1,2,3}rand = 3 {1,3,2} {2,3,1}rand = 1 {1,2,3}rand = 2 {1,3,2}rand = 3 rand = 2 rand = 3 {2,1,3} {1,2,3} rand = 2 rand = 1 {3,2,1}rand = 1 {1,3,2}rand = 2 {1,2,3}rand = 3 {2,1,3} {3,1,2}rand = 1 {2,3,1}rand = 2 {2,1,3}rand = 3 {2,3,1} {1,3,2}rand = 1 {2,1,3}rand = 2 {2,3,1}rand = 3 rand = 2 rand = 3 {3,2,1} {2,3,1}rand = 1 {1,3,2}rand = 1 {2,1,3}rand = 2 {2,3,1}rand = 3 {3,2,1} {1,2,3}rand = 1 {3,1,2}rand = 2 {3,2,1}rand = 3 {3,1,2} {2,1,3}rand = 1 {3,2,1}rand = 2 {3,1,2}rand = 3 rand = 2 rand = 3 rand = 3
  • 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.