Siemens Interview Question Software Engineer in Tests
0of 0 votesWrite a function to shuffle a deck of 52 cards. Explain how you would test that the deck was properly shuffled
!EXCLUSIVE!
<object width="445" height="364"><param name="movie" value="http://www.youtube.com/v/ibYMAWi_yq8&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ibYMAWi_yq8&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="445" height="364"></embed></object>
Наталья Немец – СОН (2009)
<object width="445" height="364"><param name="movie" value="http://www.youtube.com/v/wCmNnBc_42Q&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wCmNnBc_42Q&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="445" height="364"></embed></object>
Наталья Немец (feat. Reflex-music) - глаза на песке (NEW 2010)
<object width="445" height="364"><param name="movie" value="http://www.youtube.com/v/gK393sl1_Ck&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/gK393sl1_Ck&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="445" height="364"></embed></object>
<object width="445" height="364"><param name="movie" value="http://www.youtube.com/v/ZoskxqgsXVY&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ZoskxqgsXVY&hl=ru&fs=1&rel=0&color1=0x5d1719&color2=0xcd311b&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="445" height="364"></embed></object>
sDF
Suppose an array of 52 elements represents of a deck of card.
iterate thru each element in the array.
for each element, use a random function to generate an integer from 0-51.
swap the contents of current element with the element at random number generated index.
that's all.
To test it, we need to make sure there are no consecutive elements in the array.
Ur answer is crap.... from algo to test.. every thing... go study harder.. the rand no has to be b/w {i, n-1} array index.. n the test has to be a test to ensure that mapping b/w slot n any card is equally likely.. ie unifrom distribution....
DeckOfCards.h:
#pragma once
#include <string>
using std::string;
class DeckOfCards
{
public:
DeckOfCards(void);
~DeckOfCards(void);
void shuffleCards();
void printDeck();
private:
const static short CARDS_PER_DECK = 52;
const static short CONTENT_COUNT = 13;
const static short TYPE_COUNT = 4;
enum Content { ONE = 0, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACKS, QUEENS, KINGS};
enum Type {SPADES = 0, HEARTS, DIAMONDS, CLUBS};
static const char* toString(Content pContent);
static const char* toString(Type pType);
struct card {
Content content;
Type type;
private:
string contToString(){
switch (content){
case 0:
return "ONE";
case 1:
return "TWO";
case 2:
return "THREE";
case 3:
return "FOUR";
case 4:
return "FIVE";
case 5:
return "SIX";
case 6:
return "SEVEN";
case 7:
return "EIGHT";
case 8:
return "NINE";
case 9:
return "TEN";
case 10:
return "JACKS";
case 11:
return "QUEENS";
case 12:
return "KINGS";
default:
return 0;
}
}
string typeToString(){
switch (type){
case 0:
return "SPADES";
case 1:
return "HEARTS";
case 2:
return "DIAMONDS";
case 3:
return "CLUBS";
default:
return 0;
}
}
public:
string toString(){
string lTemp(contToString());
lTemp.append(" of ");
lTemp.append(typeToString());
return lTemp;
}
};
card deck[CARDS_PER_DECK];
};
DeckOfCards.cpp:
#include "DeckOfCards.h"
#include <cstdlib>
using std::rand;
#include <ctime>
using std::time;
#include <iostream>
using std::cout;
using std::endl;
DeckOfCards::DeckOfCards(void)
{
}
DeckOfCards::~DeckOfCards(void)
{
}
void DeckOfCards::shuffleCards(){
srand(static_cast<unsigned int> (time(0)));
card *lCard = new card();
for(int i = 0; i < CARDS_PER_DECK; i++){
lCard->content = Content(rand() % CONTENT_COUNT);
lCard->type = Type(rand() % TYPE_COUNT);
deck[i] = *lCard;
lCard = new card();
}
}
void DeckOfCards::printDeck(){
for(int i = 0; i < CARDS_PER_DECK; i++){
cout << "Card[" << i << "]: " << deck[i].toString() << endl;
}
}}
Than you just call shuffleCards() and printDeck()...

{
- amdube on May 18, 2011 Edit | Flag Replyfor (int i = ary.Length – 1; i >= 0; i++)
{
int r = rnd.Next(0, i + 1);
int tmp = ary[i];
ary[i] = ary[r];
ary[r] = tmp;
}
}