Microsoft Interview Question for Software Engineer in Tests






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

I think divide and conquer will work for this problem.

- Mike August 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My Approach would be

#1
for(1 to N)
{
random=generate a random no b/w 1 and N ;
swap element 1 and element random ;
}
Complexity O(n)

- amit.h1b August 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes

- guri August 21, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<=N,i++)
{
replaceIndex=i+random()*(N-1);
//.... replace element with (N-i)th index with replaceIndex
}

This is Kenneth algorithm which is most of the time used for shuffling.

- RKB August 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think your code not gonna work either
because if random() function generates any value greater than 1 which simply means ur replacing index is now Greater than N which will give an error .
its better take % N of Replace index and then Replace . RKB Please give me KENNETH ALGO link from whr u have referred thanks in ADVANCE

- Anonymous September 13, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

try searching for the Knuth algo, not kenneth.

- Anonymous September 23, 2008 | Flag
Comment hidden because of low score. Click to expand.
-2
of 0 votes

http://www.careercup.com/question?id=57862

- AD September 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry about that...wanted to copy

http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

- AD September 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks AD the links is really helpful

- BC December 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 i,j,mid,k;
mid=n/2;
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[]={1,2,3,4,5,6,7,8,9,10},i;
printf("before \n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
suffle(a,10);
printf("\nafter \n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
return 0;
}

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

int myRandomNumber()
{
int temp_id=0;
unsigned int myseed;
myseed = (unsigned)time(NULL);
srand(myseed); // Initialize the random number generator
temp_id = rand(); // Random number
return temp_id;
}
by seeding u will get a truly random number..

- generate this numer twice per swap..

random1 = myRandomNumber();
random2 = myRandomNumber();

swap(a[random1], a[random2]);

- Anonymous April 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

updating: random1 = random1 % lengthofArray(a)
random2 = random2 % lengthofArray(a)

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

for(i=list.size()-1;i>=0;i--)
{
int r=rand()%i;
swap(list[r],list[i]);
}

- Anonymous September 05, 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