Amazon Interview Question for SDE-2s


Country: India
Interview Type: Phone Interview




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

#include <iostream> 
#include <algorithm>
#include <vector>

using namespace std; 

enum CardSuite
{
    CARD_CLUB, 
    CARD_HEART,
    CARD_SPADE, 
    CARD_DIAMOND
};

enum CardNumber 
{
    CARD_ONE = 1,
    CARD_TWO,
    CARD_THREE,
    CARD_FOUR,
    CARD_FIVE,
    CARD_SIX,
    CARD_SEVEN,
    CARD_EIGHT, 
    CARD_NINE,
    CARD_TEN,
    CARD_JACK,
    CARD_QUEEN,
    CARD_KING

};

class OneCard
{
    CardSuite eSuite_;
    CardNumber eNumber_;
    public : 
        
        OneCard(CardSuite suite, CardNumber num)
        {
            eSuite_ = suite;
            eNumber_=num;
        }
        CardSuite GetSuite()
        {
            return eSuite_;
        }
        CardNumber GetNumber() 
        {
            return eNumber_;
        }
        void Decompile()
        {
            cout << eSuite_ << " " << eNumber_ << endl;
        }
};

class CardDeck
{
    vector <OneCard> cardDecks_; 
    public:
        CardDeck()
        {
            CardSuite suite;
            CardNumber cNumber;
            for (suite = CARD_CLUB; suite != CARD_DIAMOND + 1; suite = static_cast<CardSuite>(suite + 1))
            {
                for (cNumber = CARD_ONE; cNumber != CARD_KING + 1; cNumber = static_cast<CardNumber>(cNumber + 1))
                {
                    OneCard card = OneCard(suite, cNumber);
                    cardDecks_.push_back(card);
                }
            }
        }

        void RandomizeCards()
        {
            std::random_shuffle(cardDecks_.begin(), cardDecks_.end());
        }

        void Decompile() 
        {
            vector<OneCard> :: iterator iter = cardDecks_.begin();
            while (iter != cardDecks_.end())
            {
                (*iter).Decompile();
                iter++;
            }
        }


};


int main( )
{ 
    CardDeck oneCardDeck;
    oneCardDeck.Decompile();
    oneCardDeck.RandomizeCards();
    oneCardDeck.Decompile();
    return 0; 
}

- Pankaj Kumar October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome design :-)

- dharmeshjogadia July 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There should be four classes:

1. CardGame
suits[4] : Suit
2. Suit
cards[13] : Card
suiteType : CardType
3. Card
cardNumber : int
type: CardType
4. CardType
type: String

- poojabasia September 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why not use an enum for CardType instead of a string? Same thing for suit. Why keep an array of 4 suits, instead of the array of 52 cards? What about Jokers (that don't have a suit, but are used in some card games?) What do you do when you shuffle the cards?

- Kevin October 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 dimensional arrays with 4 rows and 13 columns

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

same Q was asked in Demand Media phone interview

- awsm31 September 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
class Card{
String cardType;
int number;
}

for CardType we can use enum.

and to create 52 cards we can have Set<Card> packOfCards

}

- kedarsdixit October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There could be a separate method living somewhere that would print out a card like "Ace of Spades" or whatever.

// Deck.h

#include "Card.h"

class Deck
{
public:
    Deck(); // Could take in an array of cards based on the type of game. Defined in .cpp
    void Shuffle(); // Defined in .cpp
    
    // What methods would we need a Deck to have?
    // How do we want to retrieve the cards? One by one?
    // Do we want to burn cards?
    // How much control do we give to other classes in the game?
    
private:
    const Card** m_ppCards; // This could also comfortably be std::vector<Card*> instead of Card**.
    int m_numCards;
};

// Card.h with inline definitions.
class Card
{
public:
    enum CARDSUIT { SPADES, HEARTS, DIAMONDS, CLUBS, NONE, NO_SUITS};
    
public:
    Card(CARDSUIT suit, int cardNumber)
    {
        m_suit = suit;
        m_cardNumber = cardNumber;
    }
    
    CARDSUIT GetSuit() const
    {
        return m_suit;
    }
    
    int GetCardNumber() const
    {
        return m_cardNumber;
    }
    
private:
    CARDSUIT m_suit;
    int m_cardNumber;
};

- Kevin October 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we use flyweights for Cards because card number is fixed from 1 to 13.

So there will not be 52 objects of Cards. There will be only 13 Card objects and 4 Suit objects.

- Vidhi Thakrar October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Deck
    {
        public enum CardSuit
        {
            Spades = 1,
            Hearts,
            Clubs,
            Diamonds,
            None
        };

        public enum CardNumber
        {
            Ace = 1,
            Two,
            Three,
            Four,
            Five,
            Six,
            Seven,
            Eight,
            Nine,
            Ten,
            Jack,
            Queen,
            King        //,Joker
        };
        public enum CardColor
        {
            Black = 1,
            Red
        };
    }
    public class Cards : Deck 
    {
        //DECK is inherited
        public Cards()
        {
            List<CardNumber> cards = new List<CardNumber>();
            List<CardSuit> suits = new List<CardSuit>();
        }

        public List<Card> NewDeal()
        {

            List<Card> dealList = new List<Card>();
            for (int n=1; n<14;n++)
            {
                for (int s = 1; s < 5; s++)
                {
                    CardSuit enumSuit = ((CardSuit) s);
                    CardNumber enumNumber = ((CardNumber)n);
                    CardColor enumColor;
                    if (s == 1 || s == 3)
                    {
                        enumColor = ((CardColor) 1);
                    }
                    else
                    {
                        enumColor = ((CardColor)2);
                    }
                    Card card = new Card();
                    card.singleCardColor = enumColor.ToString();
                    card.singleSuit = enumSuit.ToString();
                    card.singleNumber = enumNumber.ToString();
                    dealList.Add(card);
                }
            }
            return dealList;
        }

        public Card DrawCard(List<Card> deck)
        {
            Card card = new Card();
            Random r = new Random();
            string cNumber = string.Empty;
            int randomNumber = r.Next(0, deck.Count+1);
            int i = 0;
            foreach (var c in deck)
            {
                if (i == randomNumber)
                {
                    card = c;
                    break;
                }
                i++;
            }
            deck.Remove(card);
            return card;
        }
        public class Card
        {
            public string singleSuit;
            public string singleNumber;
            public string singleCardColor;
        }
    }

- Anonymous April 14, 2014 | 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