Sunteți pe pagina 1din 11

1.

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

Answer: There are multiple ways to go about this. Let s 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; }

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. Let s see who isup for the challenge. Answers will be posted February 26th. Let s 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 are the same every time, trying again won t affect their probabilities. 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

int rand7() { while (1) { int num = 5*(rand5()-1) + rand5(); if (num < 22) return ((num % 7) + 1); } }

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 Kadane s 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 subarray 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. Here s 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;

} } *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 doesn t 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. Let s 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 2 3 4 5 6 100 51 35 29 25 21

7 8 9 10 11 12 13 14 15 16

20 19 19 19 19 19 19 20 20 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, let s first try at floor 14. If it breaks, then we need 13 more tries to find the solution. If it doesn t 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 doesn t 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 at support@mytechinterviews.com. 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.

Let s 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 let s 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 at support@mytechinterviews.com. 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.

If you have any questions, please feel free to send me an email at support@mytechinterviews.com. 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. Let s start by creating an equation. Let x 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 minute window) Probability of a car passing in a 20 minute window = 1 minute window)^4 0.9 = 1 (1 1 x=1 questions. (1 x)^4 x)^4 = 0.1 x = 10^(-0.25) 10^(-0.25) = 0.4377 (1 probability of a car passing in a 5 (probability of no car passing in a 20

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

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 can t store everything, count the number of bytes and pick one. That would be too easy wouldn t it? Let s think it through. We don t 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 shouldn t 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

probability of being picked (we can use any programming language s 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 won t 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. Let s 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 let s 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 let s 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 cake s height is same at all points

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

Three kids

Jack: Hey, how are you man? Bill: Not bad, got married and I have three kids now. Jack: That s 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 don t know. Bill: My eldest kid just started taking piano lessons. Jack: Oh now I get it. How old are Bill s kids? Answer: Let s break it down. The product of their ages is 72. So what are the possible choices? 2, 2, 18 2, 4, 9 2, 6, 6 2, 3, 12 3, 4, 6 3, 3, 8 1, 8, 9 1, 3, 24 1, 4, 18 1, 2, 36 1, 6, 12 sum(2, 2, 18) = 22 sum(2, 4, 9) = 15 sum(2, 6, 6) = 14 sum(2, 3, 12) = 17 sum(3, 4, 6) = 13 sum(3, 3, 8 ) = 14 sum(1,8,9) = 18 sum(1, 3, 24) = 28 sum(1, 4, 18) = 23 sum(1, 2, 36) = 39 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.

2, 6, 6 3, 3, 8

sum(2, 6, 6) = 14 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.

S-ar putea să vă placă și