Amazon Interview Question
Software Engineer / Developersthe question is to write psuedo code for creating a deck of cards. most decks have a 52 cards with 13 cards per suite. just add the two joker cards after the deck is created instead of shuffling the deck. If you google for create a deck of cards program you should be able to find many solutions
Quick question. Does the deck have to be shuffled ?
If not then the question is trivial. First create 13 cards of heart, then spade, then diamonds and then clubs.
If the initial deck itself is shuffled, then it is a bit tricky. I would suggest creating the normal deck before then use some kind of bit map to randomly generate cards out of the deck
the interviewer didn't say anything about shuffling. they just wanted the candidate to write pusedo code for creating the deck of cards
exactly!! but instead of creating thirteen for each design which will make 13*4 loops, we can create 4 cards of each design per value which would make 13 iterations.
foreach a (1..13)
foreach b (1..4)
doSomething(a, b)
Has the same complexity as
foreach a (1..13)
doSomething(a, 1);
doSomething(a, 2);
doSomething(a, 3);
doSomething(a, 4);
clrs the logic is correct but backwards, it should be:
int rank = 13;
int suite = 4;
foreach(0...suite){
//add new suite
foreach(0...rank){
//add new card
}
}
add two jokers
this is more efficent becuase the main for loop will cycle only 4 times
plus i made a little error in my psuedo code it should be:
Card[] deck;
int rank = 13;
int suite = 4;
String
foreach(0 <= suite){
foreach(0 <=rank){
//in card class add the suite and rank string value
deck[number of cards] = new Card(suite, rank);
}
}
add two jokers
can u explain the question bit more?
- Anonymous March 05, 2010