Microsoft Interview Question for Software Engineer / Developers






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

We can use Drustenfeld's Algorithm -- Time - O(n) and Space - O(1)

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<time.h>
using namespace std;

class Deck {
public:
	int array[52];
	Deck() {
		for(int i=0;i<52;i++)
			array[i] = i+1;
	}
	
	void shuffle();
	void swp(int * begin, int * ebd);
	void print();
};

void Deck::shuffle() {	
	srand(time(NULL));
	for(int i = 51;i>=0;i--) {		
		int r = rand() % (i+1);	
		swp((int *)(array+i),(int *) (array+r));
	}

}

void Deck::swp(int * num1,int * num2) {
	int temp = *num1;
	*num1 = *num2;
	*num2 = temp;
	return;
}

void Deck::print(){
	for(int i=0;i<52;i++){
		cout<<array[i]<<" ";
	}
	cout<<endl;
}

int main() {
	Deck obj;
	obj.shuffle();
	obj.print();
	return 0;
}

- DigitalFire March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Store a card number from by 1 to 52 in an array.

Run a loop and generate a random number in range of 1 to 52. Swap loopCounter and random Number. In this way you can do swapping in just one iteration..........

- Kunal Gaind December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<iostream>
using namespace std;

int getrand(int lim)    //One flavour of random number generator
{
	static long a = 1;  //seed
	a = (a*32719 + 3)%32749;
	return (a%lim + 1);
}

int main()
{
	int cards[52],rand;
	for(int i=0;i<52;i++) //assign initial position
	{
		cards[i] = i+1;
	}
	for(int i=0;i<52;i++) //swap indexes with counters and random numbers
	{
		int temp = 0;
		rand = getrand(51);
		cout <<rand <<"\t";
		temp = cards[rand];
		cards[rand] = cards[i];
		cards[i] = temp;
	}
	cout <<endl <<endl;
	for(int i=0;i<52;i++)
		cout <<cards[i] <<"\t";
			
	return 0;
}

- Gajanan December 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not efficient at all!
It requires 52 times and each time only exchanges two cards. It is very likely, some cards are still in original positions.

Here is some ideas(acting like a dealer)

Assuming you have Spade A-10-K, following by Heart, Diamond and Club.
1. Divided the cards by 2 groups, Cards 0 to 25(Spade, Heart) and 26 to 51(Diamond, Club). Exchange the old number cards of two groups. In this exchange, Spade cards mix with Diamond cards, Heart cards mix with Club cards.
2. Divided the new deck by 3 groups. Cards 0 to 16, 17 to 33, 33 to 50 with last card left. Exchange cards in first group and the third group and then move second group to the top. Put last card radomly between card No.11 to No. 43.
3. Repeat step 2 and 3 two more times.
4. You can write a program and compare the two methods.

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

some changes in gajanan code , would make it right

int main()
{
int cards[52],rand, subset[52];
for(int i=0;i<52;i++) //assign initial position
{
cards[i] = i+1;
}
for(int i=0;i<52;i++) //swap indexes with counters and random numbers
{
int temp = 0;
rand = getrand(51);
cout <<rand <<"\t";
temp = cards[rand];
cards[rand] = cards[i];
cards[i] = temp;
subset[i]=temp;
}
cout <<endl <<endl;
for(int i=0;i<52;i++)
cout <<subset[i] <<"\t";

return 0;
}

- sam January 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check CMH to understand how to permute an array randomly. All the above mentioned ideas are very naive. They don't suffice for a precise answer.

- Anonymous January 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about inserting them into the hash table with random hash function?

- Anonymous January 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

well guys...MS won't allow u to use random function in this question...i faced this question.

- justgotit January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

well guys...MS won't allow u to use random function in this question...i faced this question.

- justgotit January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose you want to randomize an array of 52 values, from 0 to 51 with no repeats, such as you might want for a deck of cards. First fill the array with the values in order, then go thru the array and exchange each element with a randomly chosen element in the range from itself to the end. It's possible that an element will be exchanged with itself, but there is no problem with that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36



// File : misc/random/deal.cpp - Randomly shuffle deck of cards.
// Illustrates : Shuffle algorithm, srand, rand.
// Improvements: Use classes for Card and Deck.
// Author : Fred Swartz 2003-08-24, shuffle correction 2007-01-18
// Placed in the public domain.

#include <iostream>
#include <cstdlib> // for srand and rand
#include <ctime> // for time
using namespace std;

int main() {
int card[52]; // array of cards;
int n; // number of cards to deal
srand(time(0)); // initialize seed "randomly"

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

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

//--- Print first n cards as ints.
for (int c=0; c<n; c++) {
cout << card[c] << " "; // Just print number
}
cout << endl;
}

return 0;
}

- Sangeet Ahuja January 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int getRandomNo(int range){
Random rand = new Random();
return rand.nextInt(range);
}

public static void cardSuffle(){
int[] a = new int[52];
for(int i = a.length -1 ; i >0 ; i--){
a[i] = i;
}
for(int i = a.length -1 ; i >0 ; i--){
int n = getRandomNo(i + 1);
int temp = a[i];
a[i] = a [n];
a[n] = temp;
}
for(int i = 0 ; i < a.length-1 ; i++){
System.out.print(" " + a[i] + " ");
}
System.out.println("");
}

- Raj November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Define most efficient.

- Anonymous December 24, 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