Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Yup. This is one way to do it:

http :/ / en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

- smffap June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hi,

The right way to do it : Space complexity : O(1), Time Complexity : O(n)

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

#define CARD_COUNT 52

void swap(int *card_arr, int index1, int index2)
{
	if(index1 != index2)
	{
		card_arr[index1] = card_arr[index1] + card_arr[index2];
		card_arr[index2] = card_arr[index1] - card_arr[index2];
		card_arr[index1] = card_arr[index1] - card_arr[index2];
	}
}

int main(void)
{
	int i;

	int card_arr[CARD_COUNT] = {0};

	for(i = 0; i < CARD_COUNT; i++)
		card_arr[i] = i+1;
	
	for(i = 0; i < CARD_COUNT; i++)
	{
		int index = rand() % (CARD_COUNT - i) + i;
		swap( card_arr, i, index);
	}

	getch();
}

- Srikant Aggarwal June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity is n^2..!

- minbog9 June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry missed the indentation

- minbog9 June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i = 0 to 51
    generate a random number in the range (i+1 to 51): @next_idx
    swap the elements at the indices i and next_idx

- ashot madatyan June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Close, but not quite, as with this you could never have 0 in the first place.

- memo June 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, that's correct. I just showed an algorithm that I implemented long ago for one of my programs where there was a limitation not to have any element in its proper position in the final shuffled set. If we still want to have such elements, we can simply change the range from (i+1) to (i), and this will allow selecting the proper element for the given position.

- ashot madatyan June 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

srand((unsigned)time(0))
for(i=0;i<=51;i++)
{
n=rand()%52;
swap(a[i],a[n])
}

}

- Maxxx June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
int card[52];
int n;
int i,c;

srand(time(NULL)); // initialize the seed randomly

printf("How many card you want to open from the deck after shuffle");
scanf("%d",&n);

for (i=0; i<52; i++)
{
card[i] = i; // fill the array in order
}

//--- Shuffle elements by randomly exchanging each with one other.
for (i=0; i<(52-1); i++)
{
int r = i + (rand() % (52-i)); // Random remaining position.
printf("rand()%(52-i):%d\n",r);
int temp = card[i];
card[i] = card[r];
card[r] = temp;
}

//--- Print first n cards as ints.
for (c=0; c<n; c++)
{
printf("%d ",card[c]);
}

getch();
}

- Gupta June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Generate the Random No. Between 0-51(eg. x and y)....select this card and put it at the top, now generate the Random no between x-y ..so on until both no get equal..
Repeat this process n time (Depend upon the your choice of shuffle )

- Arun Kumar June 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-- you can do it with divide and conquer technique ...
-- In the merging , instead of normal mearging ... you include your own merging procedure(which will take 1 element from first array and another from the second array ..)
so final array will shuffled randomly ...

- Anonymous November 07, 2013 | 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