MI
BAN USER- 0of 0 votes
AnswersFive tree in a row and one monkey. monkey can jump left or right. find out best strategy to kill monkey. large amount of ammunition available.
- MI in United States
Some suggest solution by two shot at every tree and start from one end.
I doubt it will fail!!
Does any strategy exist ??| Report Duplicate | Flag | PURGE
- 1of 1 vote
AnswersTotal number of steps are given (let N)
- MI in India
at a time you can take one or two steps..
Q total number of ways to reach N.
My Ans: find out total number of non -ve solution to the
equation X + 2Y = N
For every pair of value, find out total number of arrangement. Ex X = x and Y = y
then arrangement is (x+y)!/(x! * y!).
I think my answer is correct but interviewer was not convince.. may be he is looking some mugged answer ..| Report Duplicate | Flag | PURGE
Amazon
void Print(struct node * root, int distance) //print node below given node ...
{
if(root == NULL || distance < 0)
return;
if(distance == 0)
printf("%d",root->data);
else
{
Print(root->left,distance -1);
Print(root->right,distance -1);
}
}
Void PrintAbove(struct node* root,int distance) //print above node from given node ..
{
if(root == NULL || distance < 0)
return;
struct node * parent = NULL;
struct node* side = NULL;
parent = root->parent;
while(parent != NULL)
{
distance--;
if(distance == 0)
printf("%d",parent->data);
else if(root == parent->left)
{
Print(parent->right,distance);
}
else
Print(parent->left,distance);
if(parent->parent == NULL) //to check other side of root ...
side = root;
root = parent;
parent = parent->parent;
}
if(distance >0)
{
if(side == root->left)
Print(root->right,distance);
else
Print(root->left,distance);
}
}
tree * LCA(tree * root, int num1, int num2)
{
if(root ==NULL)
retrun root;
else
{
if((root->data == num1) ||(root->data ==num2))
return root;
else
{
tree* left = LCA(root->left,num1,num2);
tree* right = LCA(root->left,num1,num2);
if(left && right)
return root;
else if(left)
return left;
else if(right)
return right;
else
return NULL;
}
}
}
- MI January 15, 2013For every value in given list insert in map which has key as first char and value
last char.
EX-1
a-->c
c-->e
e-->f
f-->a
now start from first element of map.
it has value c
look for value at key=c, it has e
look for value at key=e, it has f
keep on doing
if total count = given list(N) and reach to first started node
then there is cycle
if total count less that N and reach to first started node then subcycle present.
PLZ comment on algo ..
void Insert(list **head , int value)
{
list * itr = NULL;
list * pre = NULL;
if(*head == NULL)
{
*head = Node(value); //make new node
(*head)->next = *head;
return;
}
else
{
itr = *head;
pre = itr;
if(itr->value >= value)
{
while((itr != *root)&&(itr->value >= value))
{
pre = itr;
itr = itr->next;
}
}
while((itr != *root)&&(itr->value < value))
{
pre = itr;
itr = itr->next;
}
pre->next = Node(value);
pre->next->next = itr;
return;
}
}
Point 2 fail .. if monkey at 2 . you shot once at 3. monkey can jump to 3.so after once shot at 3 monkey can be at 3 also
- MI February 07, 2013