Microsoft Interview Question for Software Engineer / Developers






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

Knuth is your friend.

- T March 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void shuffle(Card[] deck) {
int numCards = deck.length;
for (int i = 0; i < numCards; i++) {
int selectedCard = (int)rand()%(numCards-i) + i;
swap(deck[i], deck[selectedCard];
}
}

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

seems you code will shuffle some card multiple times while not touching others, is that ok?

- Ryan Zhang July 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void shuffle(Card[] deck) {
    int numCards = deck.length;
    for (int i = 0; i < numCards; i++) {
        int selectedCard = (int)rand()%(numCards-i) + i;
        swap(deck[i], deck[selectedCard];
    }

}

- Anonymous March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anonymous, it does not look like your algorithm generates all permutations with the same probability.

For instance, I think Card[0] has more than 50% chance of being in the right half.

Basically, you keep shifting Card[0] to the right...

- T March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here it algo which is commonly used.

Even though it looks very similar to above code, it is different.

// We fill the array starting from the right end.
// First we select a random card from N, and place it in Card[N]
// Then we selected a random card from remaining N-1 and place it is Card[N-1]
// etc...
// This is O(n) time algorithm.

void fisherYatesShuffle(Card [] deck) {
    
    int numCards = deck.Length;
    for (int i = numCards; i > 0; i--) {
        int selectedCard = GetRandomInteger(1, i); // Get random integer in {1,2,..., i}
        Swap(deck[i-1], deck[selectedCard-1]);
    }
}

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

Could you please specify why it is different from the first one above? It seems that this algorithm keeps pushing the last one to the left......

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

Could you please specify why it is different from the first one above? It seems that this algorithm keeps pushing the last one to the left......

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

T is right. The solution before T had non-proportional probability for index 0 to be selected since the initial set starts from 1 element to N at the end. Whereas T's solution starts from N elements and shrinks down to 1 at which point probability is more uniform.

- Ashish Kaila March 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi T,

Could you please specify whether this algorithm is different from the first one above? Seems that it keeps pushing the last one to the left......

- Anonymous March 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi T,

Could you please specify whether this algorithm is different from the first one above? Seems that it keeps pushing the last card to the left......

- Bo March 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I wasn't sure if it the above algorithm (by Anonymous) was wrong, but now I think that it is in fact correct! Thanks Bo...

For a proof (of anonymous algorithm):

Consider chances that a particular card is in position 0. it is 1/n.
Chances that a particular card is in position 1. is it (n-1)/n * 1/(n-1) = 1/n
and so on...

basically same as the fischer yates shuffle.

Sorry for causing confusion.

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

Hi
I see that the argument is (Card[] deck)
I quite don't understand it. type def int data type as Card?

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

Just an array of Card objects. Why is it a problem? Are you looking at it from 'C' eyes?

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

#include<iostream>
#include<vector>

using namespace std;

int rand()
{
static int i=32676;

i=(i*269+321)%123678+17;


return i;

}

void shuffle(vector<int> list,int k)
{

int i=0,temp=0;
int sz=list.size();
int p=0;

while(i++<k)
{
temp=rand()%sz;

swap(list[(p++)%sz],list[temp]);

}


for(i=0;i<sz;i++)
cout<<list[i]<<"\t";

return;

}

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

LOL.

A 'random' program.

- LOLer July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, My son !! Its a random Programme. Like dad, Like son!!

- LOLer Daddy July 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess I got my 'wit' gene from mom.

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

void Shuffle() {
        int bagSize = SUITS_PER_DECK * CARDS_PER_SUIT;
        int index = 0;
        srand(time(NULL));
        while (bagSize) {
            index = rand() % bagSize;
            swap(cards[--bagSize], cards[index]);
        }
    }

- mg August 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

knuth algo O(n).....as stated by T is most popularly used!!!

- sd December 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

123678

- Anonymous May 23, 2022 | 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