Google Interview Questions

Google Interview Questions ::

Total there are five Technical Interviews followed by Management round.

So here are the questions.

Google Interview Round 1 ::

  1. What is the Space complexity of quick sort algorithm? how do find it?

    Solution: Quicksort has a space complexity of O(logn), even in the worst case, when it is carefully implemented such that
    * in-place partitioning is used. This requires O(1).
    * After partitioning, the partition with the fewest elements is (recursively) sorted first, requiring at most O(logn) space. Then the other partition is sorted using tail-recursion or iteration.
    The version of quicksort with in-place partitioning uses only constant additional space before making any recursive call. However, if it has made O(logn) nested recursive calls, it needs to store a constant amount of information from each of them. Since the best case makes at most O(logn) nested recursive calls, it uses O(logn) space. The worst case makes O(n) nested recursive calls, and so needs O(n) space.

    However, if we consider sorting arbitrarily large lists, we have to keep in mind that our variables like left and right can no longer be considered to occupy constant space; it takes O(logn) bits to index into a list of n items. Because we have variables like this in every stack frame, in reality quicksort requires O(log2n) bits of space in the best and average case and O(nlogn) space in the worst case. This isn't too terrible, though, since if the list contains mostly distinct elements, the list itself will also occupy O(nlogn) bits of space.


  2. What are dangling pointers?

    Solution: A dangling pointer is a pointer to storage that is no longer allocated. Dangling pointers are nasty bugs because they seldom crash the program until long after they have been created, which makes them hard to find. Programs that create dangling pointers often appear to work on small inputs, but are likely to fail on large or complex inputs.


  3. Given that you can take one step or two steps forward from a given step. So find the total number of ways of reaching Nth step.

    Solution:The simple recurrence relation governing this problem is f(N)=f(N-1) +f(N-2)(why?),which is a fibonacci sequence.
    Nth state can be arrived directly by taking 2 step movement from N-2 or 1 step from N-1.Remember N-2 -> N-1 -> N is not a direct path from N-2th state to Nth state.Hence the no of solutions is no of ways to reach N-2th step and then directly taking a 2 jump step to N + no of ways to reach N-1th step and then taking 1 step advance.


  4. You are given biased coin. Find unbiased decision out of it?

    Solution:Throw the biased coin twice.Classify it as true for HT and false for TH.Both of these occur with probability=p*(1-p),hence unbiased. Ignore the other 2 events namely HH and TT.



  5. On a empty chessboard, a horse starts from a point( say location x,y) and it starts moving randomly, but once it moves out of board, it cant come inside. So what is the total probability that it stays within the board after N steps.




Google Interview Round 2 ::

  1. You have 1 to N-1 array and 1 to N numbers, and one number is missing, you need to find the missing the number. Now you have 1 to N-2 numbers, and two numbers missing. Find them.

    Solution:
    The question can be elucidated as follows.Given an array of size N-1 containing numbers less than N and with out any duplicates!! We knew that there is a number missing from the array say K .Let S be the sum of the elements of the array.

    Sum of first N natural numbers=N*(N+1)/2

    and S=N*(N+1)/2 - K.Now putting this other way around we get K=N*(N+1)/2 -S !!



    Now the second part of the question says that there are 2 of the first N numbers missing.Let they be X and Y.

    We solve this problem by solving 2 essential equations.



    They are X+Y=N*(N+1)/2 -S---------->(1)

    X*Y=N!/P-------------------(2) where S and P are the cumulative sum and product of the array entries.

  2. You have cycle in linked list. Find it. Prove that time complexity is linear. Also find the node at which looping takes place.

    Solution:
    The problem of checking whether there is a cycle or not can be solved using 2 pointers one moving in increments of 1 and the other in increments of 2.If there is a cycle then these 2 pointers meet at some node say N1 inside the cycle otherwise the fast pointer reaches the end of the list.This is a O(N) solution.

    Now coming to the identification of the node at which looping took place.After our identification of cycle ,both the pointers P1 and P2 are at node N1.Now iterate the slow pointer to count the no of nodes in the cycle.(After traversing the whole cycle P1 and P2 shall again be at the same node).Let this size be K.Now take one of the pointers to the head node and count the no of nodes till N1.Let this number be X.Now use one of these pointers to reverse the cycle starting from N1.Only the cycle gets reversed.Now again traverse from head node to N1.Let the number of nodes this time be Y.Let the no of nodes from head to the start node of the cycle be Z

    Now X+Y=2*Z+K .Hence solve for K and then having figured out the start node N2 of the cycle.Now as the cycle is reversed having figured out this start node its next node is the looping nodes so set the looping nodes next pointer to NULL and reverse the list further till you reach N2.


  3. Questions on my project please be prepare well about your project


  4. How do you search for a word in a large database.
  5. How do you build address bar in say gmail. i.e. if you press 'r' then you get all email starting from 'r', and if you press 'ra' then you will get emails starting from 'ra'.

Google Interview Round 3 ::

  1. You have given an array. Find the maximum and minimum numbers in less number of comparisons.

    Solution:
    only 3n/2 comparisons are necessary to find both the minimum and the maximum. To do this, we maintain the minimum and maximum elements seen thus far. Rather than processing each element of the input by comparing it against the current minimum and maximum, however, at a cost of two comparisons per element, we process elements in pairs. We compare pairs of elements from the input first with each other, and then compare the smaller to the current minimum and the larger to the current maximum, at a cost of three comparisons for every two elements.

  2. You have given an array from 1 to N and numbers also from 1 to N. But more than one number is missing and some numbers have repeated more than once. Find the algo with running time O(n).

    Solution:All the numbers are positive to start with.Now, For each A[i], Check the sign of A[A[i]]. Make A[A[i]] negative if it's positive. Report a repetition if it's negative.Finally all those entries i,for which A[i] is negative are present and those i for which A[i] is positive are absent.


Google Interview Round 4 ::

  1. Three strings say A,B,C are given to you. Check weather 3rd string is interleaved from string A and B.
           Ex: A="abcd" B="xyz" C="axybczd". answer is yes.


    Solution:

    bool test(A,B,C)
    {
    i=j=k=0;
    while(k < C.size())
    {
    if(i < A.size() && C[k]==A[i])
    {i++,k++;
    }
    else if(j < B.size() && C[k]==B[j])
    {
    j++,k++;
    }
    else
    return false
    }
    return (i == A.size() && j == B.size());
    }

    The above algorithm doesn't work when C[k]=A[i]=B[j], essentially throwing one in to a dilemma whether to accept the character from A or B.


  2. Given two sorted arrays A and B.
    1. Find the intersection of these arrays A and B.

      Solution:The intersection can be found by using a variation of merge routine of the merge sort.


    2. If array A is small and array B is too large. how will you proceed for getting intersection of those two arrays?

      Solution:In this case for each entry of smaller array,we can run a binary search routine on the larger one to know its presence.

Google Interview Round 5 ::

  1. If you get into Google, which products are you going to work on?
  2. What is TCP, UDP. what is reliability, unreliability, give examples of these?

    Solution:
    Click Here To Read About TCP
    Click Here To Read About UDP
    Click Here To Read About Reliability

  3. What is http protocol?

    Solution:
    Click Here To Read About HTTP

  4. How does Google search engine works?
  5. What is indexing, what is the input and output to it. how Google does that?

This was the interview i got from one of my friends. Discuss them here. If you had attended Google Interview share you interview experience with us.You can post the questions through comments section or you can mail me at kchaitanyya@gmail.com

11 comments:

  1. Is it the same interview for new graduates ? Because I'll have the Google Japan interview on monday and now... I'm freaking out :S

    ReplyDelete
  2. hmm..it's not same for all, This interview was done by Google india for New graduates here. I am not sure how the interview process in japan would be, but i am sure that these questions may help you before going to interview.

    ReplyDelete
  3. The answer I thought of for finding the missing number in a 1 .. N-1 array has lower time complexity than the one given here. It works as follows:

    Perform a modified version of a binary search on the array. Create a lower pointer to the head of the array and an upper pointer to the tail of the array. Start by looking at the middle element of the array, (N-1)/2. If it is equal to (N-1)/2, hence it is less than the missing number, move the lower pointer to the element. If it is greater than (N-1)/2, hence it is greater than the missing number, move the upper pointer to the element. Then repeat by looking at the element halfway between the pointers; if it is equal to its index, move the lower pointer to it, else move the upper pointer to it. Repeat until the pointers are adjacent to each other; the missing number is array[lower pointer] + 1.

    This algorithm has time complexity of O(log n) since it is basically a worst-case binary search. The summation algorithm has a time complexity of O(n), because it has to add up all the elements of the array. So this algorithm is better.

    ReplyDelete
  4. unfortunately for the above solution, the array would have to be sorted, which would require additional effort and much greater time complexity. I think the one given in the post is a good one. It doesnt need a sorted array

    ReplyDelete
  5. An alternative solution to problem Interview 4, problem 1 involves backtracking.
    #include
    #include
    bool Interleaved(char *a, char *b, char *c)
    {
    if(*c==0 && (*a!=0 || *b!=0))
    return false;
    if(*a == 0 && *b == 0 && *c == 0)
    return true;
    if(*a==*c && *b!=*c)//If no ambiguity move ahead
    return Interleaved(a+1, b, c+1);
    else if(*a!=*c && *b==*c)//If no ambiguity move ahead
    return Interleaved(a, b+1, c+1);
    else if(*a==*c && *b==*c)//Else test which case gives true in the long run.
    return Interleaved(a+1, b,c+1) || Interleaved(a, b+1, c+1);
    return false;
    }
    int main()
    {
    char a[100], b[100], c[100];
    strcpy(a, "abcd");
    strcpy(b, "adcd");
    strcpy(c, "adcadcbd");
    if(Interleaved(a,b,c))
    printf("Is interleaved\n");
    else
    printf("Is not interleaved\n");
    }

    ReplyDelete
  6. I haven't attended yet...but thanx for posting...it is really going to help me in my interview...!!!!

    ReplyDelete
  7. thank u for sharing..

    ReplyDelete
  8. Nice post. I really like it. Thank you for sharing.

    Upcoming Bank Exams

    ReplyDelete
  9. you done a good job.thanks for that.its very useful

    ReplyDelete
  10. Hi

    Tks very much for post:

    I like it and hope that you continue posting.

    Let me show other source that may be good for community.

    Source: Free interview questions

    Best rgs
    David

    ReplyDelete