Thursday, February 25, 2010

Hungry Elephant


This one is easy and most famous puzzle.
But I have a doubt here which I would post later after getting answers .

There are 2 cities A and B, distance between them is 1000 Km.
we have 3000 bananas in city A and a elephant
can carry max 1000 bananas at any given time and it needs to eat a banana every 1 Km.

How Many Max. No. of bananas you can transfer to city B ?.

Wednesday, February 24, 2010

Boat and Dock


You are Sitting in a small boat holding the end of a rope.The other end of the rope is tied to the top of
a nearby pier (See figure) , such that the it is higher above the water than your end of the rope,Causing your boat to move towards the pier,stopping directly underneath the pier.As you pull on the rope, which is moving faster :boat or the rope?
i.e the speed of the boat moves across the water or the speed the rope moves through your hands.

Looks like difficult one but not exactly.

Tuesday, February 23, 2010

PJ Puzzle


In which direction is the bus moving?
Right or Left?

Monday, February 22, 2010

Blind Man and Cards


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?

Sunday, February 21, 2010

Adobe Interview Round2

Only 2 questions where asked.
we had to write complete algorithm for them.

1) int a[]= {1,1,1,2,2,3,3,3,3,4,5};
o/p shud be 1,3,2,2,3,4,4,1,5,1

i.e 1 followed by how many times 1 is repeated.

2) a ={1,1,1,2,2,3,3,3,4,4,5};
b={1,2,2,3,6};

o/p c = {6,5,4,3,2,1};

Adobe Interview Round1

1) Basic C++ question .A class definition was given and then objects of that class were created in main function.
like
class string
{
int a;
int b;
int c;
void print()
{
printf("%d%d%d",&a,&b,&c);
}

};

main()
{
string s1,s2;
s1.a=s1.b=s1.c=10;

s1 =s2;
s1.print();
string s3=s2;
s3.print();
s2.print();
}

basically he was trying to get whether i understood what happens when s1=s2 and at string s3=s2;

2) What is the difference between a big endian and little endian machine?
Can you write a code to identify them.

sol int x=1;

if(x<<1)
printf("big endian");
else "little endian";


3)Do you know what is inorder traversal and BFS.
given a sample tree do that.
write code for inorder.
I wrote recursive then he asked me for iterative.

4)An array of numbers is given which are sorted you are also given a number 'X'.
you need to print to 2 elements of array such that there sum is X.


since the elements are sorted , to search for any element we can for binary search. now subtract x from a[0] and search for the result in array.
this wud cost me o(nlogn)


5) Given i/p "My name is khan" o/p shud be "khan is my name"

one approach wud be to use linked list , wherein we use each node to store a word.

my->name->is->khan.

Now do linked list reverse operation.

by using this even multiple space between words can be taken care of.
my->space,space->name->...
i.e in the node which stores space i will store that many space characters which are present.

Interviewer asked me to get a solution without extra memory usage.
DO string reversal nahk si eman ym
now do word by word reverse khan is name my

6)char a[]="string"; char *p="string";
there are several difference b/w a and p
most imp one is that u can do free(p) but not free(a);

Tuesday, February 16, 2010

How will you measure?

You are given 2 containers one is of 5 liters capacity and other is 3 liters.
Measure 4 liters using the above two containers only.(You are given unlimited supply of water , containers are unmarked).


Start from the 5 liters can fill it and empty it in to 3 liters.
So now you have 2 liters in 5 liter can and 3 liters can is full.
Empty 3 liters can . And move 2 liters from 5 liter can to 3 liter can.
Now 3 liter can has 2 liters and 5 liter can is empty.
Now Again fill 5 liter can . Transfer as much as you can in 3 liters can.
Since you are left with only 1 liter volume in 3 liter can you can transfer only 1 liter into it.So you are left with 4 liters in 5 liters can now.

50-> 23 -> 20 -> 02 -> 52 -> 43.


Mathematically

5x+3y = 4
x = 2 y =-2 Therefore |x| + |y| = 4

i.e we need to find x and y values such that |x|+|y| is minimum.

So x here means take water 2 times from 5 liter can and drain out water from 3 liter can 2 times.

One more

There is an ample supply of milk in a milk tank. You have a 5-liter and a 9-liter cans, both of them unmarked. How can you measure out 2 liters?.

5X + 9Y = 2

X = 4 and Y = -2

50 -> 05 -> 55 -> 19 -> 10 -> 01 -> 51 -> 06 -> 56 -> 29.

done!!