Amazon Interview Question for Software Engineer / Developers






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

Classic!

enum Suit { SPADES, CLUBS, HEARTS, DIAMONDS, };
enum Face { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, };

class Card {
public:
    Card(Suit suit, Face face) : suit(suit), face(face) {}
    Card(const Card& orig) : suit(orig.suit), face(orig.face) {}
    Suit getSuit() const { return suit; }
    Face getFace() const { return face; }
private:
    Card() {}
    Suit suit;
    Face face;
    friend class Deck;
};

class Deck {
public:
    Deck() {
        int index = 0;
        for (int i = 0; i < SUITS_PER_DECK; ++i) {
            for (int j = 0; j < CARDS_PER_SUIT; ++j) {
                index = i * CARDS_PER_SUIT + j;
                cards[index].suit = (Suit) i;
                cards[index].face = (Face) j;
            }
        }
    }

    Deck(const Deck& orig) {
        for (int i = 0; i < SUITS_PER_DECK * CARDS_PER_SUIT; ++i) {
            cards[i] = orig.cards[i];
        }
    }

    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]);
        }
    }

    static const int SUITS_PER_DECK = 4;
    static const int CARDS_PER_SUIT = 13;
private:
    Card cards[SUITS_PER_DECK * CARDS_PER_SUIT];
    friend ostream & operator<<(ostream&, const Deck&);
};

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

nice approach but there r some C++ errors:

1st is no default constructor in Card and you are taking arrays of cards in ur Deck class..so Deck Card will call default cons of class but fail to find it..hence error..

- googler November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

- hello August 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

private void Shuffle(int seed)
{
Random r = new Random(seed);
for(int i=51; i>0;i--)
{
int x = r.nextInt(i); // picks from 0...i
swap cards x and i;
}

}

- Knuth baba January 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One simple solution is, assign random number to each card. and then sort the deck.

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

excellent work above. Thanks.

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

You're welcome.
About the shuffle method, mine does it in O(n), sorting will do it in O(nlogn)

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

simple and smartest solution is to assign random number ( as some one already mentioned) and then make a BST of those numbers...so we're done in O(n)...as traversing the full list takes no more than O(n)...done!

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

isnt it that in shuffle, ith cell goes to (2i%l) th position ?

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

@bebo ??????

test case guys : when after shuffling comes back to original state.

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

@ Anonymous :

Cool Stuff bro/sis!!!

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

sis can never be so smart!

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

x=0;
while input list empty {
  r= Random(x,n)
  Place rth element in xth position of the output list
  Delete entry for rth element in the input list
  x++
}
return output list.

Help me know the pitfall of this approach.

- Ankush Bindlish September 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Ankush Bindlish on September 04, 2009:
I guess you're not supposed to delete the element at the rth position as in that case you loose that element, so I guees you meant exchange the elements at x and r positions. right???

- badayun October 09, 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