Microsoft Interview Question for Software Engineer in Tests






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

How about shuffling them using the Knuth's Donald shuffling algo

- Rashi Gupta October 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any reference to this algo online ? Do you know if the randomness employed by this algo has a uniform distribution ?

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

shuffle()
{
int a[52]=(0...51};
java.util.Random rd = new Random();
int i=51;
while(i>=0)
{
int val = rd.nextInt(i+1);
a[val]=a[val]^a[i];
a[i]=a[val]^a[i];
a[val]=a[val]^a[i];
i--;
}
}

- cougar_cs October 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//(More or less same code as above)

import java.util.Random;
public class Main {

public static void main(String[] args) {
int[] a = new int[52];
int i, temp, temp2;

for(i=0;i<52;i++) // Inialize the array
{
a[i] = i;
}

Random rd = new Random();

for(i=0;i<52;i++)
{ //Shuffle the array
temp = rd.nextInt(52);
temp2 = a[i];
a[i] = a[temp];
a[temp] = temp2;
}

System.out.println("The Sorted array is");

for(i=0;i<52;i++)
{
System.out.println(a[i]);
}
}

}

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

C implementation

#include<stdio.h>
#include<time.h>

#define TOTALCARDS 52

void Shuffle(int a[], int size);
void printarray(int a[], int size);
void Swap(int *, int *);

int main()
{
int a[TOTALCARDS];
int i=0;

for(i=0;i<TOTALCARDS;i++)
{
a[i] = i+1;
}


printf("Original Array");
printarray(a, TOTALCARDS);

Shuffle(a, TOTALCARDS);

printf("Shuffled Array");
printarray(a, TOTALCARDS);



getch();
return(0);

}

void Shuffle(int a[], int size)
{
int randnum,i=0;

srand((unsigned int) time(NULL));

for(i=0;i<size;i++)
{
randnum=rand()%(size-1);
Swap(&a[i], &a[randnum]);
}

return;

}

void printarray(int a[], int size)
{
int i=0;
for(i=0;i<size;i++)
{
printf("\na[%d] = %d", i, a[i]);
}

printf("\n\n\n");
return;
}

void Swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return;
}

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

I think your permutation generate N^N numbers, but the total possibilities should be N!, so the distribution is not uniform.

Have a look at this: http://en.wikipedia.org/wiki/Shuffling

- roger September 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void swap(int *a,int x)
{
int temp=a[x];
a[x]=a[x-1];
a[x-1]=temp;
}
void suffle(int *a,int n)
{
int mid=n/2;
int i,j,k;
for(i=0;i<mid-1;i++)
{
j=i;
k=0;
while(k<=i)
{
swap(a,mid+j);
j=j-2;
k++;
}
}
}
main()
{
int a[52],i;
suffle(a,52);
printf("\nafter suffle :\n");
for(i=0;i<52;i++)
printf("%d ",a[i]);
return 0;
}

- rajnesh November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try it's good solution

- rajnesh November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't get the question. Do you?

- T March 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@T you seem cynical just like me :) And I see your comments in MSFT questions.. did you make it?

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

http://www.fredosaurus.com/notes-cpp/misc/random-shuffle.html

- prolificcoder April 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

TRY the KNUTH SHUFFLE

int t1,t2;
for(i=0;i<52;i++)
{
t1=rand()%52;
t2=rand()%52;

swap(cards[t1],cards[t2]);

}

- Anonymous July 25, 2009 | 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