Google Fresher Interview Questions – round 4

 

  • Given n non overlapping intervals and an element. Find the interval into which this element falls.

    Solution: 
    we can extend binary search to intervals.(Assuming the intervals are sorted)
    consider interval [a,b].
    if (a-x)(b-x) <=0 
    then x belongs to [a,b].
    else
    if x>a 
    element can be present only in the intervals to its right. 
    so select the middle interval among them to it’s right
    and repeat the procedure.
    else
    element can be present only in the intervals to its left. 
    so select the middle interval among them to it’s left
    and repeat the procedure.

    The complexity of this problem is log(N) where N is the number of sorted non-overlapping intervals.

  • Worst case is take all intervals one at a time and see whether the element lies in the interval or not.It will take O(n). So please give a solution that will do better than O(n).
  • Now given that the n intervals are overlapping then how do you solve? The interviewer was concentrating more on the complexities (running, memory ..) 

    Solution: 
    If the above intervals are overlapping ,then they can be merged in O(N) and then the exact intervals can be resolved later.Otherwise ,we can identify one correct interval and then linear search on its left and right neighbourhood to find the other solutions.

  • Write code for Random Sort? 
    Algorithm  is explained:
    
    Given an input array of size n. Random sort is sampling
    a new array from the given array and check whether the
    sampled array is sorted or not. If sorted return else
    sample again. The stress was on the
    code.

  • Tags: , ,


    Did You Enjoy This Post?

    Subscribe to our blog through our RSS feed or email to receive updates on more posts like this.post on your favourite social networking or media site to let others know about this post. Help us generate more buzz by submitting/voting for this post with the following buttons.

    Leave a Reply











    request new questions/article: send email to freequestionbank at gmail dot com