**A**. I couldn’t solve this question at the first shot. My proposal for hiker was to come down to 100 m hook, tie the rope at 100 m point, climb back to top and untie other end of rope. Since, we know hook is in middle, he can tie this other end to himself do a bungee jump to reach the ground

**Interviewer gave me some hints**: No jumping risk required. Bring knife to equation. We can ignore rope used for tying knots.

Gradually, reached to a workable solution.

**Right Answer:**

1) Divide the rope in 100m, 50m parts. For doing this, hiker can go down to middle point, mark the rope and come back on top. Use knife for making parts.

Objective: From the middle point, we need to use 100m part. Challenge is to reach middle point, and still have hold of 100m rope.

2) Tie one end of 50m rope to topmost cliff, and make a hanging knot at other end.

Hanging Knot

3) Fold 100 m rope to two stripes of 50m (don’t cut). Pass the hanging knot through the fold. At the end, middle of 100 m part should be touching knot point, both ends are kept together.

Hook1———————-()================Hook2

200m 150m 100m

4). Hiker has to hold both ends of 100m part. The length of assembly is 100m. He can reach to Hook2.

5). At hook 2, release one end of the rope and keep pulling from the other end. Since, 100m part is not tied tightly, it will come out of the knot and hiker will get this 100 m part to cover rest distance.

6) Use 100 m part to get down from middle point.

]]>

A.

- Examine digits starting from zeroth place and move towards higher order. Stop at the point where current digit is less than previous one. Say this location is j. In example 4365, the digit is ‘3’.

- If no such element met and all digits occurred in ascending order, throw error saying that number is maximum possible with given digits.
- The digits examined so far (from 0th position to j-1) are sorted. Do binary search on same window and spot the element just greater than digit at location j. In example, its ‘5’.
- Swap these digits (Example: 45632)
- Examined digits (from 0th position to j-1) is still sorted. Change the order by reversing the sequence. (Example: sorted 45 ‘236’)
- The resulting number is the answer

Complexity O(n).

Spotting the element takes O(logn) as its binary search.

Sorting takes O(n) because we are just reversing the digits sequence.

]]>

A. Maintain a min heap of size n. A location pointer will be maintained for checked elements in each row.

Drop 1st element from each row, move the location pointer then 2nd row.

Pull out the minimum element, note the row from which it belonged and put the next element from the same row in the heap.

Repeat the process K times. Kth smallest element will occur at that stage.

]]>

Eg. “Harsh is awesome!” should be reversed to “awesome! is Harsh”.

A. The trick here is to handle the spaces. The best solution I could think of was to reverse all the characters and than reverse each individual word.

Step 1 – !emosewa si hsrah

Step 2 – awesome! is harsh

]]>

1. Val(Y) < Val(x)

2. Position(Y) < Position(X)

3. Position of Y should be maximum as possible

4. If there is no such Y, set it to null

Try to achieve complexity better than O(n^2).

A: Use a doubly linked list with following insertion and maintenance rules –

1. Linked starts with null and at any point, parent node is the value of Y for operational element. The prints of Y happens at the time of element insertion.

2. For each element X, Start with the tail of the linked list.

> If its null node, add a node X as its successor.

> If linked list node element is less than X, add a new node as its successor.

> If node element is greater equals X, delete that node(including its tail) and re-do comparison with its parent.

Sample run

Say for array 7, 1, 4, 8, 5, 6, 2

Operational Element | Linked List | Operation taken | Printed Result (X, Y) |

7 | null -> 7 | Added node 7 | 7, Null |

1 | null -> 1 | Deleted node 7 and added node 1 | 1, Null |

4 | null -> 1 -> 4 | Added node 4 | 4,1 |

8 | null -> 1 -> 4 -> 8 | Added node 8 | 8,4 |

5 | null -> 1 -> 4 -> 5 | Deleted 8 and added 5 | 5,4 |

6 | null -> 1 -> 4 -> 5 -> 6 | Added Node 6 | 6,5 |

2 | null -> 1 -> 2 | Deleted 6, then 5, then 4 while traversal from tail and added 2 | 2,1 |

PS: We do complete traversal of linked list from right to left. We can have a step further by maintaining the linked list in such a manner, that we can do binary search on it (by storing position index separately). It can be more optimized by using binary search as linked list is always sorted.

]]>

People will be lined up in a queue 1, 2, 3… N. At first phase, people at odd locations will be eliminated. Same thing will apply for next phase.

For example,

Round 1: 1, 2, 3, 4, 5, 6….N

Round 2: 2, 4, 5.. N (People at odd locations eliminated)

Round 3: 4, 6… N (People at odd locations eliminated)

Y0u look charming. So, you get a choice for your position. Given the value of N, where will you position yourself to take up the job ??

A. I would choose position is the last power of 2 within N

for example, if N=17, the last power of 2 is 16 (2^4). Why ?? Think and tell me

]]>

A. One guard says truth and other lies. If we take cumulative result, we’ll always get a lie (as atleast one of them lies). So, I will ask to guard “Will the other guard say if this door is correct ?”. One of these two guys will definitely lie which makes right door is exactly opposite to the question’s answer.

]]>

Starting from the last, jailer will ask each prisoner what is the color of his own hat. If prisoner gives wrong answer, he will shoot him right at that moment.

Prisoners have to come up with a strategy such that maximum of them can be saved.

**A. **

The last prisoner will say black if there are odd number of black hats in front of him. For simplicity, lets assume they are in odd number.

- Second last prisoner can count the black hats in front of him. If he gets odd number, he can conclude the hat on his head is white and black otherwise.
- Third last prisoner, will keep his ear open on the previous answer and then he’ll calculate of total black hats in front of him and behind him. That way he can decide if total is a odd number and can predict his hat’s color.
- Using this technique, n-1 prisoners can be guaranteed to be saved.

]]>

- Sum of all the elements should result into n.
- Multiplication of all the elements should be greater than any other similar set (whose elements result n when summed up).

A. This is a very interesting problem as we have to go through every combination and calculate and compare the multiplication. Since, there is no restriction on the length of the subset, it makes problem much more interesting.

I propose use of dynamic programming here. We will start computing the f(n) from 1

At every stage, we have to compute all combinations of f(1).. f(n-1)

f(n) = max {

f(1) * f(n-1)

f(2) * f(n-2)

….. }

To avoid repeated operations, we can store start computing f(1).. f(n-1) one by one and do the book keeping of the result.

]]>

Should be printed as {A, B, C, G, F, E, D, H, I, J, K , L, M}

A. We have to use two stacks for doing this operation

- At every level, we will push the nodes in one stack and print them.
- At every push, print the node.
- At every pop, push the children in another stack.
- When the stack is empty change the traversal direction. i.e. if its left and then right node, it will be changed as right and then left node.

For example, above stack will result in following operation

- Push A in stack 1. A printed.
- Pop A and push B n C in stack 2. B and C printed. Direction is left -> right.
- Change the direction and child stack reference. Direction becomes right -> left and stack 1 becomes the new child stack.
- Pop C, this will print and push G and C. Since direction is right, left now, G is taken up first.
- Similar operation keeps repeating till both stacks are empty.

]]>