# Engineering internship Interview Questions

# 22K

Engineering Internship interview questions shared by candidates### You are in a boat in a pool with a rock in your hand. You throw the rock into the pool. Does the water level rise, drop, or stay the same?

13 Answers↳

If the rock were neutrally buoyant the water level would remain the same. It is heavier than water which causes it to displace more than its own volume while in the boat compared to at the bottom of the lake. Therefore the water level of the lake would go down. Less

↳

These answers are troubling. The only correct answer so far is Ben. The water level goes down. Less

↳

The weight of the boat plus you plus the rock has already displaced the height of the water. The only time the water level will change will be when the rock is mid air. Less

### Gave a Situation of a Fire Burning in company. 3 Options : a) Run out of the company b) Save other People c ) Call fire Service

26 Answers↳

Answer was : Follow the evacuation procedure (Run out of the comany)

↳

call fire service and save the people

↳

Call fire service

### You have 25 horses, what is the minimum number of races you can find the top 3. In one race you can race 5 horses, and you don't have a timer.

31 Answers↳

7. chzwiz, your answer is incorrect. One of the first or second place horses could be 2nd fastest and/or 3rd fastest. I drew a grid to visualize the problem. First, run five races to establish 5 groups of internally ranked horses, and you can of course immediately eliminate 4 & 5 of each race. 1 2 3 x x 1 2 3 x x 1 2 3 x x 1 2 3 x x Then race 1st place horses, eliminate 4 & 5, and those they beat earlier. You can also eliminate the horses #3 beat, and the 3rd place horse from #2's first race. 1 ? ? X X 2 ? X X X 3 X X X X X X X X X X X X X X You know #1 is fastest. Race the remaining horses (2, 3 and ?'s), and the top two are 2nd and 3rd. After reading the above answers, this is the same as B's revised answer, but I found it easier to explain/visualize with the grid. Less

↳

The answer is definitely 7, here is a fantastic explanation: http://www.programmerinterview.com/index.php/puzzles/25-horses-3-fastest-5-races-puzzle/ Less

↳

It would be 7, mainly because since there only 5 racers each round, then it would be tournament style. As you can tell, tournament style never determines the rankings in the short term. (Imagine if a new tennis player played against Roger Federer and he loses. He's not bad, he's just really unlucky haha) So in order to determine the answer, you would have to use massive logic and reasoning. First step is obvious: you get 5 sets of horses to race through 5 races. So the number is already at 5. We get the top horse in each one and note them. However, one problem is that you have to consider if fastest horses could lie all in the first, second, third, fourth, or fifth group. But we can't determine that yet, so let's race one more time with the winners of each race. Second step: Race with each winner from their groups. That makes 6 total races so far. Ok, now we can get somewhere with this. So now, its a question of which horses we can eliminate to determine the next race. Who can we eliminate though? Well, we can get rid of: - All the horses in the group with the last race that were 4th and 5th place. This makes sense since if the winner of their groups only made 4th and 5th place in the tournament style race, then they are definitely not the fastest horses. 15 horses remaining. - Well, we can also get rid of the last two horses of each group since we are only looking for the top 3. 9 horses remaining. - We also can get rid of the first place winner since we already know he is definitely the best. 8 horses remaining. - Since we already determined who the fastest horse is, now we have to determine who the second and third fastest horse can be. In that case, the second place horse from the last round can get rid of the third horse of that group, since he definitely has no shot of being the first or second fastest horse of this new race. Also, in the third group, we can get rid of the second and third horse in the group due to the same reasoning above. 5 horses remaining. Oh wow! We now have 5 horses left! This means we can just have a clean race and find the second and third fastest horse. So overall, the answer is 7 races! Less

### Given a number n, find the largest number just smaller than n that can be formed using the same digits as n.

16 Answers↳

package interview; import java.util.Arrays; import java.util.Scanner; public class LargestNumberBeforN { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); for (int i = a.length() - 1; i > 0; i--) { if (a.charAt(i) = 0; i--) { b = b + String.valueOf(a[i]); } return b; } } Less

↳

Approach. Traverse from the last till u get a number that is greater than previous number.. Put last digit before this new number and the rest of digits in reverse order after this greater number. #include #include using namespace std; int main() { int n,newN,newNumber=0,old=9; cin>>n; int numberToAdd=n%10; int i=0; n=n/10; while(n>0) { newN=n%10; if(old Less

↳

My answer did not show properly: public static int smaller(int n){ if(n less than 10) return n; int d0 = n % 10, n2 = n / 10, d1 = n2 % 10; if(d1 greater than d0) return n / 100 * 100 + d0 * 10 + d1; return smaller(n2) * 10 + d0; } Less

### The first question he gave me was not hard. 1. You call to someone's house and asked if they have two children. The answer happens to be yes. Then you ask if one of their children is a boy. The answer happens to be yes again. What's the probability that the second child is a boy? 2. (Much harder) You call to someone's house and asked if they have two children. The answer happens to be yes. Then you ask if one of their children's name a William. The answer happens to be yes again.(We assume William is a boy's name, and that it's possible that both children are Williams) What's the probability that the second child is a boy?

12 Answers↳

1. BB, BG, GB, GG 1/4 each, which later reduced to only BB, BG, GB with 1/3 probability each. So the probability of BB is 1/3 2. Let w is the probability of the name William. Probability to have at least one William in the family for BB is 2w-w^2, For BG - w, GB - w, GG - 0. So the probability of BB with at least one William is (2w-w^2)/(2w+2w-w^2) ~ 1/2 Less

↳

The answer by Anonymous poster on Sep 28, 2014 gets closest to the answer. However, I think the calculation P[Y] = 1 - P[C1's name != William AND C2's name != William] should result in 1 - (1- e /2) ( 1- e / 2) = e - (e ^ 2 ) / 4, as opposed to poster's answer 1 - (e^2) / 4, which I think overstates the probability of Y. For e.g. let's assume that e (Probability [X is William | X is boy]) is 0.5, meaning half of all boys are named William. e - (e ^ 2) / 4 results in probability of P(Y) = 7/16; Y = C1 is William or C2 is William 1 - (e ^ 2) / 4 results in probability of P(Y) = 15/16, which is way too high; because there is more than one case possible in which we both C1 and C2 are not Williams, for e.g. if both are girls or both are boys but not named William etc) So in that case the final answer becomes: (3e/2 - (e^2)/2) * 0.5 / (e - (e ^ 2) / 4) = 3e - e^2 / 4e - e^2 = (3 - e) / (4 - e) One reason why I thought this might be incorrect was that setting e = 0, does not result in P(C2 = Boy | Y) as 0 like Anyoymous's poster does. However I think e = 0 is violates the question's assumptions. If e = 0, it means no boy is named William but question also says that William is a Boy's name. So that means there can be no person in the world named William, but then how did question come up with a person named William! Less

↳

I think second child refers the other child (the one not on the phone) In this case answer to first is 1/3 and second is (1-p)/(2-p) where p is total probability of the name William. For sanity check if all boys are named William the answers coincide. Less

### Implement a power function to raise a double to an int power, including negative powers.

11 Answers↳

#include #include #define MAX_ARRAY_LENGTH 256 double power(double, unsigned int); int main(int argc, char** argv) { double a = atof(argv[1]); int b = atoi(argv[2]); double result = power(a, b >> 31 == 0 ? b : -b); if ((unsigned int) b >> 31 == 1) { result = 1 / result; } printf("%f\n", result); return 0; } double power(double a, unsigned int b) { switch (b) { case 0: return 1.0; case 1: return a; default: return (b & 1) == 0 ? power(a * a, b >> 1) : power(a * a, b >> 1) * a; } } Less

↳

I am surprised that not a single person here had noticed that the guy asked to raise a DOUBLE to a given power. Men, double are not integers. Their exponent is stored in a part of their binary representation. If you multiply n times a double you will make n times a rounding error and n useless calculations. Just changed the binary part of the double that is related to its exponent, and here it is, your double has been raised to a given power, a you absolutely lost no precision, and you've made 0 calculations. This is basic stuff, every university teaches that to its students... floating numbers representation... Less

↳

TD's answer is interesting, but not very useful. If you actually try it you'll find that since the double's base is 2, any changes to the exponent portion approximately multiply (or divide) numbers by a power of two. I say approximately here, since TD forgot to mention that the number itself isn't stored in float point numbers, only the digits after the implied 1. So yes, it's important to know how floating point numbers work, but modifying the exponent portion of a floating point number is a fundamentally incorrect solution. Less

### Given an array of numbers, find all combinations of two numbers whose sum is a given number n.

11 Answers↳

keep track of numbers you encounter in hash table. as you walk through array, check if (n-current) number exists in hash table. if so, return that combination. O(n) run time Less

↳

I do not understand you either. Why do you need a hash table? What for? Google would not accept n^2 solutions. The most popular solution would be in NlogN. First sort the array. Then look for j s.t. a[i] + a[j] = n. Use I-index to iterate through the array and use bin sort to find a[j] = n - a[i]. This will also work for k-sum. Less

↳

I don't see how the above solution is O(n). Can you be more elaborate?

### You are given an array with n positive integers where all values in the array are repeated except for one. Return the one that is not repeated.

9 Answers↳

public static int notRepeated(int[] given) { Map m = new HashMap(); for(i=0; i < given.length; i++) { if(m.get(given[i])) { m.put(given[i], 2); } else m.put(given[i], 1); for(x:m) { if(x == 1) return x; } } } Less

↳

Cant use XOR as it fails when any element repeats odd number of times..... Can use hash map with O(n) time and O(n) space Less

↳

only 2 possible solutions 1.) using sorting 2.) using additional space. which will be less than o(n) Less

### There is an NxM grid containing a robot at (1, 1) and a destination at (N, M). Robot can move only up or right. Some locations can have obstacles. Find the number of unique paths from (1, 1) to (N, M). What is the time complexity of your algorithm?

9 Answers↳

Not quite. There are O(N + M) steps, but to reach the goal you have N steps 'up' and M steps 'right' (or vice versa, it doesn't really matter), the combinations of which are a subset of all possible 'up'/'right' combinations. Consider a dynamic programming approach instead. Less

↳

The answer is the binomial coefficient (N + M choose N) (equivalently, N + M choose M). You can actually use this graph visualization to prove the mathematical lemma that N + M choose N = N + M choose M. So in a sense, the optimal solution operates in constant time and space, by just calculating that binomial coefficient. This is what you'd do in a programming competition. But if you wanted to actually search the graph, you could use dynamic programming: if we are at a node with north neighbor N and east neighbor E, our answer is 1 + the number of paths from N + the number of paths from E. To optimize this, we want to reverse the situation and go backwards from (N,M) to (1,1). This enables us to cache previous calculations (memoization). Pseudocode: Create a map numPaths to remember previous calculations. Set numPaths(N,M) = 0. Calculate numPaths(N - 1, M) and numPaths(N, M - 1) in terms of numPaths(N,M) and then numPaths(N - 1, M - 1) = 1 + numPaths(N - 1, M) + numPaths(N, M - 1). Rinse and repeat, as you go in rectangular waves from the upper right to upper left. At the end, numPaths(1,1) will be known. Less

↳

*lower left. Sorry for the typo.