Microsoft Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

We can shuffle it by the donald knuth's shuffle algo.
Time complexity will be O(n lg n).
Possible Test case is that we are not shuffling the number out of array boundary.
Hope the samedesign work for casino too.

- Ratn October 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check wiki page on shuffling.
http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms
Linear time shuffling algorithm.

- kshitij October 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>

int main()
{
int array[52];
int i, temp;
int temp2;

for(i=0;i<52;i++)
array[i] = i;

for(i=0;i<52;i++)
{
temp = random()%51;
temp2 = array[i];
array[i] = array[temp];
array[temp] = temp2;
}

printf("\n Shuffled Array is");

for(i=0;i<52;i++)
{
printf("%d",array[i]);
printf("\n");
}

return 0;

}

- Nachiketha October 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if a random number is generated multiple times? it won't work for that... you can use another array to keep track of the randomly generated numbers.

- Cheema April 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shuffling algorithms

In a computer, shuffling is equivalent to generating a random permutation of the cards. There are two basic algorithms for doing this, both popularized by Donald Knuth. The first is simply to assign a random number to each card, and then to sort the cards in order of their random numbers. This will generate a random permutation, unless two of the random numbers generated are the same. This can be eliminated either by retrying these cases, or reduced to an arbitrarily low probability by choosing a sufficiently wide range of random number choices.

The second, generally known as the Knuth shuffle or Fisher-Yates shuffle, is a linear-time algorithm (as opposed to the previous O(n log n) algorithm if using efficient sorting such as mergesort or heapsort) which involves moving through the pack from top to bottom, swapping each card in turn with another card from a random position in the part of the pack that has not yet been passed through (including itself). Providing that the random numbers are unbiased, this will always generate a random permutation.

- Wikipedia November 05, 2008 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More