Today we continue with a few more puzzles.
A blind man is handed a deck of 52 cards and told that exactly 10 of these cards are facing up. How can he divide the cards into two piles, not necessarily of equal size, with each pile having the same number of cards facing up?
The blind man splits the cards into two piles of 10 and 42. The pile of ten may have k cards placing up. The pile of 42 may have 10 - k cards. Then he flips over the pile of ten cards so that it has 10 - k face up cards.
A lab test for a disease has an accuracy of 99%. If it tests positive for a disease that affects 1 in 10000, what is the chance that the disease is present.
By Bayes theorem, this is equal to
P(A/B)P(B) + P(A/ not B) P(not B)
= 0.99 × 0.0001 + 0.01× 0.9999
= 0.01
A and B play a number game. Either of them can start playing. They choos a number between one and ten both inclusive. And increment the choice to the last number uttered. Can A have a winning strategy ?
Yes as long as there is a sum available to reach the destination say 11, A wins. Working backwards, A starts from 2.
Tom and Jerry race along a circular track. Jerry has a head start of 1/8 before Tom starts. Jerry is at the start when Tom covers 1/6 th. How much faster does Tom need to run to win the race.
Jerry has a head start of 3/24
Jerry covers an additional 7/8 - 1/6 = 17/24
Jerry is faster than Tom by 17/4.
Tom has to move 5/6 while Jerry has to move 1/6
Therefore Tom must travel 5 times faster which is 85/4
A chooses a number between 1 and 10000. B must guess it in as few chances as possible. On each guess B is told whether it is less or more. If B's guess exceeds A's choice two or more times, he loses. What strategy could B have.
B chooses squares. 1, 4,9, 16, ... if the number exceeds once, he goes sequential from the previous square.
#codingexercise
Find the number of islands in a binary matrix.
This could be generalized to a connected components in a graph problem.
int GetCountIslands(Int [, ] board, int row, int col)
{
return count;
}
A blind man is handed a deck of 52 cards and told that exactly 10 of these cards are facing up. How can he divide the cards into two piles, not necessarily of equal size, with each pile having the same number of cards facing up?
The blind man splits the cards into two piles of 10 and 42. The pile of ten may have k cards placing up. The pile of 42 may have 10 - k cards. Then he flips over the pile of ten cards so that it has 10 - k face up cards.
A lab test for a disease has an accuracy of 99%. If it tests positive for a disease that affects 1 in 10000, what is the chance that the disease is present.
By Bayes theorem, this is equal to
P(A/B)P(B) + P(A/ not B) P(not B)
= 0.99 × 0.0001 + 0.01× 0.9999
= 0.01
A and B play a number game. Either of them can start playing. They choos a number between one and ten both inclusive. And increment the choice to the last number uttered. Can A have a winning strategy ?
Yes as long as there is a sum available to reach the destination say 11, A wins. Working backwards, A starts from 2.
Tom and Jerry race along a circular track. Jerry has a head start of 1/8 before Tom starts. Jerry is at the start when Tom covers 1/6 th. How much faster does Tom need to run to win the race.
Jerry has a head start of 3/24
Jerry covers an additional 7/8 - 1/6 = 17/24
Jerry is faster than Tom by 17/4.
Tom has to move 5/6 while Jerry has to move 1/6
Therefore Tom must travel 5 times faster which is 85/4
A chooses a number between 1 and 10000. B must guess it in as few chances as possible. On each guess B is told whether it is less or more. If B's guess exceeds A's choice two or more times, he loses. What strategy could B have.
B chooses squares. 1, 4,9, 16, ... if the number exceeds once, he goes sequential from the previous square.
#codingexercise
Find the number of islands in a binary matrix.
This could be generalized to a connected components in a graph problem.
int GetCountIslands(Int [, ] board, int row, int col)
{
int count = 0;
bool visited = new int[row, col]();
for (int i = 0; i < row; i ++)
for (int j = 0; j < col; j++)
if (board[i,j] && !visited(i,j))
{
count++;
DFS(board,row, col, i, j, ref visited);
}
}return count;
}
void DFS(int[,] board, int row, int col, int i, int j, ref int[,] visited)
{
var rows = {-1, -1, -1, 0, 0, 1,1,1};
var cols = {-1, 0, 1, -1, 1, -1, 0, 1};
visited[i,j] = true;
for (int k =0; k < 8; k++)
if (isSafe(board, i + rows[k], j + cols[k], ref visited))
DFS(board, row, col, i+rows[k], j+cols[k], ref visited);
}
Find the maximum width of a binary tree where the width is the count of nodes at a given level
int GetMaxWidth(Node root)
{
int max = 0;
for (int i =l; i < height(root); i++){
int count = GetWidth(root, i);
if (count > max)
max = count;
}
return root;
}
int GetWidth(Node root, int level)
{
if (root == null) return 0;
if (level == 1) return 1;
if (level > 1){
return GetWidth(root.left, level -1) + GetWidth(root.right, level-1);
}
return 0;
}
{
var rows = {-1, -1, -1, 0, 0, 1,1,1};
var cols = {-1, 0, 1, -1, 1, -1, 0, 1};
visited[i,j] = true;
for (int k =0; k < 8; k++)
if (isSafe(board, i + rows[k], j + cols[k], ref visited))
DFS(board, row, col, i+rows[k], j+cols[k], ref visited);
}
Find the maximum width of a binary tree where the width is the count of nodes at a given level
int GetMaxWidth(Node root)
{
int max = 0;
for (int i =l; i < height(root); i++){
int count = GetWidth(root, i);
if (count > max)
max = count;
}
return root;
}
int GetWidth(Node root, int level)
{
if (root == null) return 0;
if (level == 1) return 1;
if (level > 1){
return GetWidth(root.left, level -1) + GetWidth(root.right, level-1);
}
return 0;
}
No comments:
Post a Comment