Myntra Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
In case there are only 2 eggs.
Lets say we have N floors and I choose a constant step size of x
No of steps to find the floor where it breaks = N/x + x
First part to identify the block size where it breaks
Second part to check for each floor in the block to identify the exact floor where it breaks
Now its just a problem of minimizing it which can be solved by differentiation, which gives x = sqrt(N)
In case of infinite eggs binary search is the right option.
There is one more dimension to this problem. It is the number of tries you are allowed.
There are three variables here -
1. No. of eggs - k
2. No. of floors - n
3. No. of tries allowed - q
This problem will then have an optimal substructure property and the recurrence will be the following -
MR is the function calculating the same -
MR(q,k)={1for k=0∨q=0
MR(q−1,k−1)+MR(q−1,k) for k>0∧q>0
MR(q,k) is the maximum number of floors 0..n−1 we can have ,if we have q tries and k eggs to break.
Note that MR(q,q+1)=MR(q,q) because the additional jar cannot be used.MR(q,q)=2^q.
You forgot to say that you have only 2 eggs.
- GENIUS February 20, 2014Keep doubling the floor you try with first egg.
Try first, 2nd, 4th, 8th, ....
Finally it will break at some 2^(k+1) th floor
But it didn't break on 2^k floor.
Now linear scan from 2^k to 2^(k+1) with second egg.
Might be better ideas?