Implement rand7() using rand5()

Problem Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. (i.e., implement rand7() using rand5()). Solution This appear to be one of those probabilistic analysis questions. You should be familiar with the concept of expected value, as it could be extremely helpful in probabilistic analysis. This solution is based upon Rejection Sampling. The main idea is when you generate a number in the desired range, output that number immediately. [Read More]

Monty hall problem - Choose the Correct Door

Problem On the game show “Let’s make a deal”, Monty Hall (the host) walks up to a contestant and gives them $100 unless they want to play “the three doors”. They usually turn down the money and play “the three doors”. Now behind one of the doors is an enourmous cash prize, behind the other too are silly prizes such as a years supply of toilet paper or a goat. So Monty asks this same contestant to pick a door, which they do. [Read More]

Key Exchange Puzzle

Problem Jan and Maria have fallen in love (via the internet) and Jan wishes to mail her a ring. Unfortunately, they live in the country of Kleptopia where anything sent through the mail will be stolen unless it is enclosed in a padlocked box. Jan and Maria each have plenty of padlocks, but none to which the other has a key. How can Jan get the ring safely into Maria’s hands? [Read More]

Russian Roulette

Problem Lets play a game of Russian roulette. You are tied to a chair and can’t get up. Here is the gun , six chambers all empty. Now I put two bullets in the gun and I put these bullets in the adjacent chambers. I close the barrel and spin it. I put the gun to your head and pull the trigger. Click and the slot was empty. Now before we start the interview I want to pull the trigger one more time , which one do you prefer , that I spun the barrel first or that I just pull the trigger ? [Read More]

NEWYORK HAIR (Pigeonhole Principle)

Problem You are visiting NYC when a man approaches you. “Not counting bald people, I bet a hundred bucks that there are two people living in New York City with the same number of hairs on their heads,” he tells you. “I’ll take that bet!” you say. You talk to the man for a minute, after which you realize you have lost the bet. What did the man say to prove his case? [Read More]

undefined

The Knight’s Tour is a famous chess problem, in which a knight starts on the top-left square of an ordinary chessboard and then makes 63 moves, landing on every square of the chessboard exactly once (except for the starting square). Can you complete the Knight’s Tour? For a further challenge, can you find a “closed” solution, meaning that the knight can make a 64th move to land back on the starting square (thus making the solution circular)? [Read More]

Brothers and sisters

Problem You and a friend are standing in front of two houses. In each house lives a family with two children. “The family on the left has a boy who loves history, but their other child prefers math,” your friend tells you. “The family on the right has a 7-year old boy, and they just had a new baby,” he explains. “Does either family have a girl?” you ask. “I’m not sure,” your friend says. [Read More]

CTRL+A, CTRL+C, CTRL+V

Code given by does not give desired output.. pleas…

Anonymous - May 3, 2014

Code given by does not give desired output.. please check the code given below working fine.

int MaxCopy(int n){
int *table=(int *)malloc(sizeof(int)*(n+1));
memset(table,0,sizeof(int)*(n+1));

for(int i=0;i<=n;i++){
table[i]=i;
}
for(int i=0;i<=n;i++){
for(int j=i+4;j<=n;j++){
table[j]=max(table[j],table[i]*(j-i-2));
}
}
int res=table[n];
free(table);
return res;
}

Thanks man for fixing it. I will update it soon.

CTRL+A, CTRL+C, CTRL+V

Problem Imagine you have a special keyboard with the following keys:  A Ctrl+A Ctrl+C Ctrl+V where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” operations respectively. If you can only press the keyboard for N times (with the above four keys), please write a program to produce maximum numbers of A. If possible, please also print out the sequence of keys. That is to say, the input parameter is N (No. [Read More]

Match two board configurations of tic-tac-toe problem

Problem How will you match two board configurations of tic-tac-toe problem? Solution This is a quite unexpected interview question as it has nothing to do with your programming skills or algorithms. This is a plain simple question designed to check your problem solving skills. Consider the tic-tac-toe configurations : The trick in this question is that any board configuration is rotation invariant and what we mean by that is all 2 the configurations shown above are similar and must match in the logic we use for checking board configurations. [Read More]