i. take the next input symbol and Enque.
ii. you can pop as many as you can. When ever you
pop an element it will be printed
(you cannot pop from an empty stack)How many such permutations are possible on an input of size N?
Solution:
It is Nth catalan number.For a detailed solution look at question5 of Stacks and Queues
For Example:
1234 is input.
First push all 1,2,3,4 on to stack and pop all.
output will be 4321.It means that this data structure can generate 4321.
Solution:
3124
for a detailed solution please look at question7 of the post
Stacks and Queues
Input: 12345
Data Structure: Deque ( Doubly Que )
Note: Deque is a data structure into which you can do enque
and deque from both sides.Some thing like this
__________________________________
enque ---> <----enque dequeue <---- ----->dequeue
__________________________________Solution:
It is N!. Guess why?(no constraints).Convince yourself by proving that every permutation can be generated by a set of valid operations.This prove can be using the principle of strong mathematical induction.So for this specific input the answer is 120.
Solution:
Let “d” be the number of drops required to find out the max floor.we need to get the value of d.
let’s say if we drop from height d then if it breaks then we have d-1 floors to check for the second egg . so max of “d” drops, so first we will drop it from height “d” if it doesn’t break at a height “d” then we are left with “d-1″ drops,so lets drop it from d + ‘d-2′ + 1 height suppose if it break there then you are left with ‘d-2′ drops.
and so on until that sum is less than 100, it’s like a linear search,
in equations,
(1+(d-1))+ (1+(d-2)) + …. >= 100
here we need to find out d
from the above equation
d(d + 1)/2 >= 100
from above d is 14
Tags: google, Interview, Question

