google interview puzzuls

Upload: vinay-pandey

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Google Interview Puzzuls

    1/11

    1. Question: Reverse a Linked-list. Write code in C.

    Answer: There are multiple ways to go about this. Lets first look at a recursive solution.

    Node * reverse( Node * ptr , Node * previous)

    {

    Node * temp;

    if(ptr->next == NULL) {

    ptr->next = previous;

    return ptr;

    } else {

    temp = reverse(ptr->next, ptr);

    ptr->next = previous;

    return temp;

    }

    }

    reversedHead = reverse(head, NULL);

    Now for a non-recursive solution.

    Node * reverse( Node * ptr )

    {

    Node * temp;

    Node * previous = NULL;

    while(ptr != NULL) {

    temp = ptr->next;

    ptr->next = previous;

    previous = ptr;

    ptr = temp;

    }

  • 8/4/2019 Google Interview Puzzuls

    2/11

    return previous;

    }

    2:Question: Write a method to generate a random number between 1 and 7, given a method

    that generates a random number between 1 and 5. The distribution between each of the

    numbers must be uniform.

    Challenge: Do you know the answer to this question? Post in the comments. Lets see who

    isup for the challenge. Answers will be posted February 26th.

    Lets think of this like a decision tree. Each rand5() will be a decision. After 2 tries, we have 25

    possible solutions. We try to get maximum bunches of 7 as we can (1 21, 3 sets of 7). If we

    get any of the other values, we just try again. Since the probability of getting each of 21 values

    are the same every time, trying again wont affect their probabilities.

  • 8/4/2019 Google Interview Puzzuls

    3/11

    int rand7() {

    while (1) {

    int num = 5*(rand5()-1) + rand5();

    if (num < 22) return ((num % 7) + 1);

    }

    }

  • 8/4/2019 Google Interview Puzzuls

    4/11

    That was fun, right? Anyone up for another challenge? Watch out for it next tuesday (March

    1st).

    3:

    Question: You are given an array with integers (both positive and negative) in any random

    order. Find the sub-array with the largest sum.

    Answer: This is an all-time favorite software interview question. The best way to solve this

    puzzle is to use Kadanes algorithm which runs in O(n) time. The idea is to keep scanning

    through the array and calculating the maximum sub-array that ends at every position. The sub-

    array will either contain a range of numbers if the array has intermixed positive and negative

    values, or it will contain the least negative value if the array has only negative values. Heres

    some code to illustrate.

    void maxSumSubArray( int *array, int len, int *start, int *end,

    int *maxSum )

    {

    int maxSumSoFar = -2147483648;

    int curSum = 0;

    int a = b = s = i = 0;

    for( i = 0; i < len; i++ ) {

    curSum += array[i];

    if ( curSum > maxSumSoFar ) {

    maxSumSoFar = curSum;

    a = s;

    b = i;

    }

    if( curSum < 0 ) {

    curSum = 0;

    s = i + 1;

  • 8/4/2019 Google Interview Puzzuls

    5/11

    }

    }

    *start = a;

    *end = b;

    *maxSum = maxSumSoFar;

    }

    4:Question:You have two identical eggs. Standing in front of a 100 floor building, you

    wonder what is the maximum number of floors from which the egg can be dropped without

    breaking it. What is the minimum number of tries needed to find out the solution?

    Answer: The easiest way to do this would be to start from the first floor and drop the egg. If it

    doesnt break, move on to the next floor. If it does break, then we know the maximum floor the

    egg will survive is 0. If we continue this process, we will easily find out the maximum floors the

    egg will survive with just one egg. So the maximum number of tries is 100 that is when the egg

    survives even at the 100th floor.

    Can we do better? Of course we can. Lets start at the second floor. If the egg breaks, then we

    can use the second egg to go back to the first floor and try again. If it does not break, then we

    can go ahead and try on the 4th floor (in multiples of 2). If it ever breaks, say at floor x, then

    we know it survived floor x-2. That leaves us with just floor x-1 to try with the second egg. So

    what is the maximum number of tries possible? It occurs when the egg survives 98 or 99 floors.

    It will take 50 tries to reach floor 100 and one more egg to try on the 99th floor so the total is

    51 tries. Wow, that is almost half of what we had last time.

    Can we do even better? Yes we can (Bob, the builder). What if we try at intervals of 3?

    Applying the same logic as the previous case, we need a max of 35 tries to find out the

    information (33 tries to reach 99th floor and 2 more on 97th and 98th floor).

    Interval Maximum tries

    1 100

    2 51

    3 35

    4 29

    5 25

    6 21

  • 8/4/2019 Google Interview Puzzuls

    6/11

    7 20

    8 19

    9 19

    10 19

    11 19

    12 19

    13 19

    14 20

    15 20

    16 21

    So picking any one of the intervals with 19 maximum tries would be fine.

    Update: Thanks to RiderOfGiraffes for this solution.

    Instead of taking equal intervals, we can increase the number of floors by one less than the

    previous increment. For example, lets first try at floor 14. If it breaks, then we need 13 more

    tries to find the solution. If it doesnt break, then we should try floor 27 (14 + 13). If it breaks,

    we need 12 more tries to find the solution. So the initial 2 tries plus the additional 12 tries

    would still be 14 tries in total. If it doesnt break, we can try 39 (27 + 12) and so on. Using 14

    as the initial floor, we can reach up to floor 105 (14 + 13 + 12 + + 1) before we need more

    than 14 tries. Since we only need to cover 100 floors, 14 tries is sufficient to find the solution.

    Therefore, 14 is the least number of tries to find out the solution.

    If you have any questions, please feel free to send me an email

    [email protected]. If you have any interview questions which you feel would

    benefit others, I would love to hear about it.

    5:

    Question: Four people need to cross a rickety bridge at night. Unfortunately, they have only

    one torch and the bridge is too dangerous to cross without one. The bridge is only strong

    enough to support two people at a time. Not all people take the same time to cross the bridge.

    Times for each person: 1 min, 2 mins, 7 mins and 10 mins. What is the shortest time needed

    for all four of them to cross the bridge?

    Answer: The initial solution most people will think of is to use the fastest person as an usher to

    guide everyone across. How long would that take? 10 + 1 + 7 + 1 + 2 = 21 mins. Is that it?

    No. That would make this question too simple even as a warm up question.

  • 8/4/2019 Google Interview Puzzuls

    7/11

    Lets brainstorm a little further. To reduce the amount of time, we should find a way for 10 and

    7 to go together. If they cross together, then we need one of them to come back to get the

    others. That would not be ideal. How do we get around that? Maybe we can have 1 waiting on

    the other side to bring the torch back. Ahaa, we are getting closer. The fastest way to get 1

    across and be back is to use 2 to usher 1 across. So lets put all this together.

    1 and 2 go cross

    2 comes back

    7 and 10 go across

    1 comes back

    1 and 2 go across (done)

    Total time = 2 + 2 + 10 + 1 + 2 = 17 mins

    If you have any questions, please feel free to send me an [email protected]. If you have any interview questions which you feel would

    benefit others, I would love to hear about it.

    6:

    Question: In a country where everyone wants a boy, each family continues having babies till

    they have a boy. After some time, what is the proportion of boys to girls in the country?

    (Assuming probability of having a boy or a girl is the same)

    Answer: This is a very simple probability question in a software interview. This question might

    be a little old to be ever asked again but it is a good warm up.

    Assume there are C number of couples so there would be C boys. The number of girls can be

    calculated by the following method.

    Number of girls = 0*(Probability of 0 girls) + 1*(Probability of 1 girl) + 2*(Probability of 2 girls)

    +

    Number of girls = 0*(C*1/2) + 1*(C*1/2*1/2) + 2*(C*1/2*1/2*1/2) +

    Number of girls = 0 + C/4 + 2*C/8 + 3*C/16 +

    Number of girls = C

    (using mathematical formulas; it becomes apparent if you just sum up the first 4-5 terms)

    The proportion of boys to girls is 1 : 1.

    Challenge yourself with more probability questions or interview puzzles.

  • 8/4/2019 Google Interview Puzzuls

    8/11

    If you have any questions, please feel free to send me an email

    [email protected]. If you have any interview questions which you feel would

    benefit others, I would love to hear about it.

    7:

    Question: The probability of a car passing a certain intersection in a 20 minute windows is 0.9.

    What is the probability of a car passing the intersection in a 5 minute window? (Assuming a

    constant probability throughout)

    Answer: This is one of the basic probability question asked in a software interview. Lets start

    by creating an equation. Letx be the probability of a car passing the intersection in a 5 minute

    window.

    Probability of a car passing in a 20 minute window = 1 (probability of no car passing in a 20

    minute window)Probability of a car passing in a 20 minute window = 1 (1 probability of a car passing in a 5

    minute window)^4

    0.9 = 1 (1 x)^4

    (1 x)^4 = 0.1

    1 x = 10^(-0.25)

    x = 1 10^(-0.25) = 0.4377

    That was simple eh. Ready for a challenge? Have a look at some of the other probability

    questions.

    8:uestion: You have a stream of bytes from which you can read one byte at a time. You only

    have enough space to store one byte. After processing those bytes, you have to return a

    random byte. Note: The probability of picking any one of those bytes should be equal.

    Answer: Since we can only store one byte at a time, we have to be able to work with the bytes

    as they come in. We cant store everything, count the number of bytes and pick one. That

    would be too easy wouldnt it?

    Lets think it through. We dont know how many bytes there are, so at any point in time we

    should have a random byte picked from all the bytes we have seen so far. We shouldnt worry

    about the total number of bytes at any time.

    When we get the first byte, it is simple. There is only one so we store it. When we get the

    second one, it has 1/2 probability of being picked and the one we have stored has a 1/2

  • 8/4/2019 Google Interview Puzzuls

    9/11

    probability of being picked (we can use any programming languages built-in random function).

    After picking one, we replace the current stored byte with the one we picked. Now it gets

    interesting. When we get the third one, it has 1/3 probability of being picked and the one we

    have stored has a 2/3 probability of being picked. We pick one of the two bytes based on this

    probability. We can keep doing this forever.

    Just generalizing, when we get the nth byte, it has a 1/n probability of being picked and the

    byte we have stored has (n-1)/n probability of being picked.

    Sometimes, this question is a little confusing. The question said you have only space to store

    one byte but there is an assumption that you have space to store variables to track the number

    of bytes and so on. Another assumption is that the stream of bytes is not infinite. If not, we

    wont be able to calculate (n-1)/n.

    If you have suggestions or comments, they are always welcome

    9:

    Question: How would you cut a rectangular cake into two equal pieces when a rectangular

    piece has already been cut out of it? The cut piece can be of any size and orientation. You are

    only allowed to make one straight cut.

    Answer: Simple question right? There are two possible solutions to this problem. People often

    overlook the easier solution to this problem. Lets start with the easiest solution. If you make

    one straight horizontal cut along the height of the cake, the resulting slices are of equal sizes.

    But this solution may not work so well on a cake with icing. So lets rethink.

    In general, when a straight cut is made at any angle through the center of a rectangle, the

    resulting pieces are always of equal area. So lets consider our situation. What if we make a

    straight cut such that it passes through the center of both the rectangles? Since the cut halves

    both the rectangles, the resulting two pieces are guaranteed to have equal area. Each piece has

    an area equal to half the original cake minus half the area of the missing rectangular piece. This

    results in two pieces of equal size, assuming the cakes height is same at all points

    10:

    uestion: Two old friends, Jack and Bill, meet after a long time.

  • 8/4/2019 Google Interview Puzzuls

    10/11

    Three kids

    Jack: Hey, how are you man?

    Bill:Not bad, got married and I have three kids now.

    Jack: Thats awesome. How old are they?

    Bill: The product of their ages is 72 and the sum of their ages is the same as your birth date.Jack: Cool But I still dont know.

    Bill: My eldest kid just started taking piano lessons.

    Jack: Oh now I get it.

    How old are Bills kids?

    Answer: Lets break it down. The product of their ages is 72. So what are the possible choices?

    2, 2, 18 sum(2, 2, 18) = 22

    2, 4, 9 sum(2, 4, 9) = 15

    2, 6, 6 sum(2, 6, 6) = 14

    2, 3, 12 sum(2, 3, 12) = 17

    3, 4, 6 sum(3, 4, 6) = 13

    3, 3, 8 sum(3, 3, 8 ) = 14

    1, 8, 9 sum(1,8,9) = 18

    1, 3, 24 sum(1, 3, 24) = 28

    1, 4, 18 sum(1, 4, 18) = 23

    1, 2, 36 sum(1, 2, 36) = 391, 6, 12 sum(1, 6, 12) = 19

    The sum of their ages is the same as your birth date. That could be anything from 1 to 31 but

    the fact that Jack was unable to find out the ages, it means there are two or more combinations

    with the same sum. From the choices above, only two of them are possible now.

  • 8/4/2019 Google Interview Puzzuls

    11/11

    2, 6, 6 sum(2, 6, 6) = 14

    3, 3, 8 sum(3, 3, 8 ) = 14

    Since the eldest kid is taking piano lessons, we can eliminate combination 1 since there are two

    eldest ones. The answer is 3, 3 and 8.

    11:

    1. Deadlocks, recovery from deadlocks, corresponding systems in databases, mutexes,

    semaphores,

    2. OOPS : polymorphism, inheritance real world examples

    3. ISO-OSI model. purpose of various layers, protocols in various layers, TCP vs UDP

    4. 2-3 puzzles. They were all from google interview questions

    5.pre-processor directives, C vs C++ vs Java

    .