Microsoft Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Pseudo code:

Init CountCards = [] all zero

for each card in poker_hand: 
   CountCards[card] ++

Sort CountCards array descreasingly.

if CountCards[0] ==4: return "four of a kind"
if CountCards[0] ==3 and CountCards[1] ==2: return "full house"
if CountCards[0] ==3: return "three of a kind"
if CountCards[0] ==2 and CountCards[1] ==2: return "two pair"
if CountCards[0] ==2: return "one pair"
return "Nothing"

- I_forgot_C++ December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Pseudo code:

Init CountCards = [] all zero

for each card in poker_hand: 
   CountCards[card] ++

Sort CountCards array descreasingly.

if CountCards[0] ==4: return "four of a kind"
if CountCards[0] ==3 and CountCards[1] ==2: return "full house"
if CountCards[0] ==3: return "three of a kind"
if CountCards[0] ==2 and CountCards[1] ==2: return "two pair"
if CountCards[0] ==2: return "one pair"
return "Nothing"

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

All you need to check is the number of distinct values in the array with some additional processing.
5 - You have nothing
4 - You have a pair
3 - You have 2 pair (max repeat = 2) / 3 of a kind (max repeat = 3)
2 - You have 4 of a kind

- YamatoGo December 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I've only been programming for about 2 weeks. So here is what I did. Any advice would be helpful. :-D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Poker
{
    class Program
    {
        static string HandOutcome(List<int> hand)
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<string> found = new List<string>();

            foreach (int i in hand)
            {
                if (!tmp.ContainsKey(i))
                {
                    tmp.Add(i, 1);
                }
                else
                {
                    int curValue = tmp[i] + 1;
                    tmp[i] = curValue;
                }
            }

            foreach (int i in tmp.Values)
            {
                if (i == 4)
                {
                    found.Add("four");
                }
                else if (i == 3){
                    found.Add("three");
                }else if (i == 2)
                {
                    found.Add("pair");
                }
            }
            
            if (found.Contains("four"))
            {
                return "You have a 4 of a kind...";
            }
            else if (found.Contains("three") && found.Contains("pair"))
            {
                return "You have a full house";
            }else if (found.Contains("three")) {
                return "You have 3 of a kind";
            }
            else if (found.Contains("pair")){
                found.Remove("pair");
                if (found.Contains("pair"))
                {
                    return "You have 2 pair!";
                }
                else
                {
                    return "You have a pair..";
                }
            }
            else
            {
                return "You have nothing...";
            }


        }
        static void Main(string[] args)
        {

            List<int> myHand = new List<int>();
            myHand.Add(2);
            myHand.Add(2);
            myHand.Add(3);
            myHand.Add(3);
            myHand.Add(3);

            string result = HandOutcome(myHand);

            MessageBox.Show(result);

        }
    }
}

- My Guess December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Poker
{
    class Program
    {
        static string HandOutcome(List<int> hand)
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<string> found = new List<string>();

            foreach (int i in hand)
            {
                if (!tmp.ContainsKey(i))
                {
                    tmp.Add(i, 1);
                }
                else
                {
                    int curValue = tmp[i] + 1;
                    tmp[i] = curValue;
                }
            }

            foreach (int i in tmp.Values)
            {
                if (i == 4)
                {
                    found.Add("four");
                }
                else if (i == 3){
                    found.Add("three");
                }else if (i == 2)
                {
                    found.Add("pair");
                }
            }
            
            if (found.Contains("four"))
            {
                return "You have a 4 of a kind...";
            }
            else if (found.Contains("three") && found.Contains("pair"))
            {
                return "You have a full house";
            }else if (found.Contains("three")) {
                return "You have 3 of a kind";
            }
            else if (found.Contains("pair")){
                found.Remove("pair");
                if (found.Contains("pair"))
                {
                    return "You have 2 pair!";
                }
                else
                {
                    return "You have a pair..";
                }
            }
            else
            {
                return "You have nothing...";
            }


        }
        static void Main(string[] args)
        {

            List<int> myHand = new List<int>();
            myHand.Add(2);
            myHand.Add(2);
            myHand.Add(3);
            myHand.Add(3);
            myHand.Add(3);

            string result = HandOutcome(myHand);

            MessageBox.Show(result);

        }
    }
}

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

namespace Poker
{
    class Program
    {
        static string HandOutcome(List<int> hand)
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<string> found = new List<string>();

            foreach (int i in hand)
            {
                if (!tmp.ContainsKey(i))
                {
                    tmp.Add(i, 1);
                }
                else
                {
                    int curValue = tmp[i] + 1;
                    tmp[i] = curValue;
                }
            }

            foreach (int i in tmp.Values)
            {
                if (i == 4)
                {
                    found.Add("four");
                }
                else if (i == 3){
                    found.Add("three");
                }else if (i == 2)
                {
                    found.Add("pair");
                }
            }
            
            if (found.Contains("four"))
            {
                return "You have a 4 of a kind...";
            }
            else if (found.Contains("three") && found.Contains("pair"))
            {
                return "You have a full house";
            }else if (found.Contains("three")) {
                return "You have 3 of a kind";
            }
            else if (found.Contains("pair")){
                found.Remove("pair");
                if (found.Contains("pair"))
                {
                    return "You have 2 pair!";
                }
                else
                {
                    return "You have a pair..";
                }
            }
            else
            {
                return "You have nothing...";
            }


        }
        static void Main(string[] args)
        {

            List<int> myHand = new List<int>();
            myHand.Add(2);
            myHand.Add(2);
            myHand.Add(3);
            myHand.Add(3);
            myHand.Add(3);

            string result = HandOutcome(myHand);

            MessageBox.Show(result);

        }
    }

}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Poker
{
    class Program
    {
        static string HandOutcome(List<int> hand)
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<string> found = new List<string>();

            foreach (int i in hand)
            {
                if (!tmp.ContainsKey(i))
                {
                    tmp.Add(i, 1);
                }
                else
                {
                    int curValue = tmp[i] + 1;
                    tmp[i] = curValue;
                }
            }

            foreach (int i in tmp.Values)
            {
                if (i == 4)
                {
                    found.Add("four");
                }
                else if (i == 3){
                    found.Add("three");
                }else if (i == 2)
                {
                    found.Add("pair");
                }
            }
            
            if (found.Contains("four"))
            {
                return "You have a 4 of a kind...";
            }
            else if (found.Contains("three") && found.Contains("pair"))
            {
                return "You have a full house";
            }else if (found.Contains("three")) {
                return "You have 3 of a kind";
            }
            else if (found.Contains("pair")){
                found.Remove("pair");
                if (found.Contains("pair"))
                {
                    return "You have 2 pair!";
                }
                else
                {
                    return "You have a pair..";
                }
            }
            else
            {
                return "You have nothing...";
            }


        }
        static void Main(string[] args)
        {

            List<int> myHand = new List<int>();
            myHand.Add(2);
            myHand.Add(2);
            myHand.Add(3);
            myHand.Add(3);
            myHand.Add(3);

            string result = HandOutcome(myHand);

            MessageBox.Show(result);

        }
    }
}

- John December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Works but I've only been programming for a little over a week so any advice would be helpful. :-)

static string HandOutcome(List<int> hand)
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<string> found = new List<string>();

            foreach (int i in hand)
            {
                if (!tmp.ContainsKey(i))
                {
                    tmp.Add(i, 1);
                }
                else
                {
                    int curValue = tmp[i] + 1;
                    tmp[i] = curValue;
                }
            }

            foreach (int i in tmp.Values)
            {
                if (i == 4)
                {
                    found.Add("four");
                }
                else if (i == 3){
                    found.Add("three");
                }else if (i == 2)
                {
                    found.Add("pair");
                }
            }
            
            if (found.Contains("four"))
            {
                return "You have a 4 of a kind...";
            }
            else if (found.Contains("three") && found.Contains("pair"))
            {
                return "You have a full house";
            }else if (found.Contains("three")) {
                return "You have 3 of a kind";
            }
            else if (found.Contains("pair")){
                found.Remove("pair");
                if (found.Contains("pair"))
                {
                    return "You have 2 pair!";
                }
                else
                {
                    return "You have a pair..";
                }
            }
            else
            {
                return "You have nothing...";

}

- CSNoob December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So sorry for the repeated. The site wasnt updating eariler and did think it was working. :-( not sure how to delete my last comments

- CSNoob December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication46
{
    class Program
    {
        static void Main(string[] args)
        {
            // get hand
            string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { ' ','\n','\r','\t'},StringSplitOptions.RemoveEmptyEntries);
            Dictionary<int, int> result = new Dictionary<int, int>();
            // count the same cards
            for (int i = 0; i<input_data.Length;i++)
            {
                var key = int.Parse(input_data[i]);
                if (result.ContainsKey(key))
                {
                    result[key]++;
                }
                else
                {
                    result.Add(key,1);
                }
            }
            // begin from the most expensive combination
            if (result.ContainsValue(4))
            {
                Console.WriteLine("four of a kind");
            }
            else if (result.ContainsValue(3) && result.ContainsValue(2))
            {
                Console.WriteLine("full house");
            }
            else if (result.ContainsValue(3))
            {
                Console.WriteLine("three of a kind");
            }
            else if(result.ContainsValue(2))
            {
                int count_pair=0;
                foreach (KeyValuePair<int, int> item in result)
                {
                    if (item.Value == 2)
                    {
                        count_pair++;
                    }
                }
                if (count_pair == 2)
                {
                    Console.WriteLine("dable pair");
                }
                else
                {
                    Console.WriteLine("pair");
                }
            }
            Console.ReadKey();
        }
    }
}

- Trushnikov.Ivan December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Executes in O(n).

Public enum
{
	None, OnePair, TwoPair, Triple, FullHouse, Quads
} Hands;

Hands GetPokerScore(int[] cards)
{
	HashTable<int, int> cardCount = new HashTable<int, int>();
	int pairCount = 0;
	bool triple; 

	foreach(int i in cards)
	{
		int count = 1;
		if(cardCount.Contains(i))
			count = cardCount(i) + 1;

		switch(count)
		{	
			Case(4):
				return Hands.Quads;
			Case(3):
				pairCount--;
				triple = true;
				break;
			Case(2):
				pairCount++;
				break;
		}

		if(pairCount > 0 && triple)
			return Hands.FullHouse;

		cardCount.put(count);

	}

	if(paircount == 2)
		Return Hands.TwoPair;
	else if pairCount == 1
		Return Hands.OnePair;

	return Hands.None;
}

- Sai December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does it in one traversal

Public enum
{
None, OnePair, TwoPair, Triple, FullHouse, Quads
} Hands;

Hands GetPokerScore(int[] cards)
{
	HashTable<int, int> cardCount = new HashTable<int, int>();
	int pairCount = 0;
	bool triple; 

	foreach(int i in cards)
	{
		int count = 1;
		if(cardCount.Contains(i))
			count = cardCount(i) + 1;

		switch(count)
		{	
			Case(4):
				return Hands.Quads;
			Case(3):
				pairCount--;
				triple = true;
				break;
			Case(2):
				pairCount++;
				break;
		}

		if(pairCount > 0 && triple)
			return Hands.FullHouse;

		cardCount.put(count);

	}

	if(paircount == 2)
		Return Hands.TwoPair;
	else if pairCount == 1
		Return Hands.OnePair;

	return Hands.None;
}

- Sai December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PokerHands {

    public void validPokerHand(int[] arr) {
        TreeMap<Integer, Integer> hmap = new TreeMap<Integer, Integer>();
        ArrayList<String> hands = new ArrayList<String>();
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (hmap.containsKey(arr[i])) {
                hmap.put(arr[i], hmap.get(arr[i]) + 1);
            } else {
                hmap.put(arr[i], 1);
            }
        }
        if (hmap.size() == 5)
            System.out.println("Poker Hand : All Different Cards");
        else if (hmap.size() == 4)
            System.out.println("Poker Hand : One Pair");
        else {
            if (hmap.size() == 3) {
                    if (hmap.containsValue(3))
                        System.out.println("Poker Hand : Three of a kind");
                    else
                        System.out.println("Poker Hand : Two Pairs");
            } else if (hmap.size() == 2) {
                    if (hmap.containsValue(4))
                        System.out.println("Poker Hand : Four of a kind");
                    else
                        System.out.println("Poker Hand : Full House");
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = { 5, 3, 3, 3, 3 };
        PokerHands ph = new PokerHands();
        ph.validPokerHand(arr);
    }

- aman December 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without even checking the rest of the answers mine look pretty similar to one of them. This is the code.

public static void PokerHand() {
			// Given an array of five integers that represents a poker hand e.g. {2,2,2,3,3} 
			// return the value of the hand, valid values are only "pair", "two pair", 
			// "three of a kind", "full house", "foud of a kind"
			int[] cards = { 1, 1, 1, 9, 1 };
			Dictionary<int, int> hashTable = new Dictionary<int, int>();
			
			// Sort cards to start
			Array.Sort(cards);
			for(int i=0;i<cards.Length;i++) {
				if(hashTable.ContainsKey(cards[i])) {
					hashTable[cards[i]]++;
				}
				else {
					hashTable.Add(cards[i], 1);
				}
			}
			
			// I could obtain a dictionary of ocurrences. Get the values and remove the 1's
			var validCards = hashTable.Values.Where(x => x > 1).ToArray();
			Array.Sort(validCards);
			
			// Valid hands are:
			// 2  |  2,2  | 3  | 3,2 || 4
			string hand_result = "";
			if(validCards.Length == 0) {
				hand_result = "Nothing!";
			}
			else if(validCards.Length == 1) {
				switch (validCards[0]) 
				{
					case 2:
						hand_result	= "Pair";
						break;
					case 3:
						hand_result = "Three of a kind";
						break;
					case 4:
						hand_result = "Four of a kind";
						break;
					case 5:
						hand_result = "Poker";
						break;
				}
			}
			else {
				if(validCards[1] == 2) {
					hand_result = "Two pairs";
				}
				else {
					hand_result = "Full house";
				}
			}
			
			Console.WriteLine("The hand is {0}", hand_result);
		}

- maximilianorios December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You have to take into account that a pair of 3's is better than a pair of 2's and so on.
You have to think of way to take into account each card count and each card value for each count. Just like so:

def valueOfHand(hand):
    count = [0] * 13

    for card in hand:
        count[card-1] += 1

    value = 0

    for card in range(13):
        if count[card] > 0:
            value += (card + 1) * 100 ** count[card]  # 100 for the numbers not overlap with the other indexes

    return value

                                    #                            count
                                    #                             4  3  2  1  0
valueOfHand([1, 1, 2, 2, 2])        # 2010000   -> 0002010000 -> 00 02 01 00 00
valueOfHand([13, 13, 13, 12, 12])   # 13120000  -> 0013120000 -> 00 13 12 00 00
valueOfHand([1, 1, 1, 1, 2])        # 100000200 -> 0100000200 -> 01 00 00 02 00

- kiamli December 27, 2015 | 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