Microsoft Interview Question for Senior Software Development Engineers


Country: United States




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

import java.util.*;
public class FisherYates {

    public static void shuffle(int[] array) {
        Random r = new Random();
        for (int i = array.length - 1; i > 0; i--) {
            int index = r.nextInt(i);
            // swap
            int tmp = array[index];
            array[index] = array[i];
            array[i] = tmp;
        }
    }


    public static void main(String[] args) {
        int [] cards = new int [52];
        for (int i = 0; i <52 ; i++) {
            cards [i]=i+1;
        }
        shuffle(cards);
        System.out.println(Arrays.toString(cards));
    }
}

- Makarand October 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Shuffle(vector<int> &cards)
{
	for (int i = 0; i + 1 < cards.size(); ++i) {
		swap(cards[i], cards[rand() % (cards.size() - i)]);
	}
}

- Alex October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>

using namespace std;
const int deckSize = 52;
void fisherYatesShuffler( vector<int>& cards )
{
	srand(time(0));
	for( int i=cards.size()-1; i>=0; --i)
	{
		int randomIndex = rand() % (i+1);
		swap( cards[i], cards[randomIndex] );
	}
	
}
int main()
{
	vector<int> cards( deckSize );
	for( int i=0; i<cards.size(); ++i)
	{
		cards[i] = i+1;
	}
	
	fisherYatesShuffler( cards );
	return 0;
}

- mr.robot.fun.society October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution

import java.util.Random;

public class ShuffleDeckOfCards {
    private static void shuffle(int[] input) {
        int inputLength = input.length;
        // class to generate random numbers
        Random r = new Random();

        for (int i = inputLength - 1; i > 0; i--) {
            // Generate random index
            int index = r.nextInt(i);

            // swap i with randomly generated index
            int temp = input[index];
            input[index] = input[i];
            input[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] cards = new int[52];
        for (int i = 0; i < 52; i++) {
            cards[i] = i + 1;
        }
        shuffle(cards);
    }
}

// Time complexity: O(N)

- Anonymous February 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in Java:

import java.util.Random;

public class ShuffleDeckOfCards {
    private static void shuffle(int[] input) {
        int inputLength = input.length;
        // class to generate random numbers
        Random r = new Random();

        for (int i = inputLength - 1; i > 0; i--) {
            // Generate random index
            int index = r.nextInt(i);

            // swap i with randomly generated index
            int temp = input[index];
            input[index] = input[i];
            input[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] cards = new int[52];
        for (int i = 0; i < 52; i++) {
            cards[i] = i + 1;
        }
        shuffle(cards);
    }
}

// Time complexity: O(N)

- annu025 February 16, 2018 | 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