Software Engineering New Grad Interview Questions | Glassdoor

# Software Engineering New Grad Interview Questions

239

Software engineering new grad interview questions shared by candidates

## Top Interview Questions

Sort: Relevance Popular Date

Sep 15, 2011
 Implement division without using multiplication or division. It should work most efficient and fast. 11 Answers exp(ln(a)-ln(b))=a/b What if one or both of a,b is less than zero. ln(x) for x < 0 is not defined. we can use bit shift operator. e.g. 4 is 100 in binary we want to divide 4 by 2 so right shift 4 by 1 bit 4>>1, so we get 010 which is 2. Show More Responses This solution rounds down to the nearest signed integer // Implement division without using multiplication or division. It should work most efficient and fast. int Divide(int divisor, int dividend) { int divisionCount; int tmp = dividend; if (tmp - divisor > 0) { tmp = tmp - divisor; divisionCount++; } // This will apply the correct sign to the quotient if ((divisor & 8) ^ (dividend & 8) != 0) { divisionCount = divisionCount | 8; } return divisionCount; } // correcting previous answer int Divide(int divisor, int dividend) { int divisionCount; int tmp = dividend; if (tmp - divisor > 0) { tmp = tmp - divisor; divisionCount++; } // This will apply the correct sign to the quotient if ((divisor & 0x80000000) ^ (dividend & 0x80000000) != 0) { divisionCount = divisionCount | 80000000; } return divisionCount; } can anyone post solution in java? public class Solution { public static void main(String[] args){ int top=32; int bottom=4; int count=0; boolean negative=(top*bottom)=bottom){ top=top-bottom; count++; } System.out.print((negative)?"-":""+String.valueOf(count)+"..."+top); } } Obviously the interviewer would not allow us to use Math functions like exp, log etc. We are supposed to use the Long division method or the Newton Raphson method to find the quotient. Newton Raphson is the fastest but uses operator * (multiplication) though. http://stackoverflow.com/a/5284915 Python version that gives you an idea how it works: i = 0 while divisor = 0: if dividend >= divisor: dividend -= divisor result |= 1 >= 1 i-=1 plus some code to check for 0 and support negative values #Write a program to do division without division or multiplication 2 3 def division(dividend, divisor_initial): 4 divisor_final = divisor_initial 5 quotient = 1 6 while dividend - divisor_final > divisor_initial: 7 quotient += 1 8 divisor_final = divisor_final + divisor_initial 9 number = divisor_final - divisor_initial 10 remainder = dividend - divisor_final 11 return quotient,remainder 12 13 14 def main(): 15 print division( 101, 3) 16 17 18 if __name__ == "__main__": 19 main()

Oct 6, 2012
 Write a function that finds the median of a set of three numbers, also find the Big O. Can it be done with only 2 comparisons, or do you need 3? 10 Answers Pick two numbers: A and B, Add the third number to each of the first two : (A+C), (B+C). Compare these two numbers and take the lesser of the two. Now compare C with the other member of the less number. The greater of these two is the median. I'm not following. Is this: say, B+C is less than A+C, then the larger of B and C is the median? If so, isn't this a counterexample: A = 2, B = 1, C = 3? Actually the answer is the next one: we could have an answer using only two comparisons. The main idea is that we need to examine one of the numbers to get into the segment created by the other two numbers. And another important thing is that one comparison could be used to definitely determine for both two segments created by two numbers. For example we are trying to examine number A and we have two segments formed by B and C numbers: [B; C] and [C; B]. But considering that for determining if A is in the segment created by B an C we need to make the following comparison: (A - B) * (C - A) >= 0. It is easy to notice that if A is in segment [B; C] (B is less or equals to C) we have both multipliers are positive but in an opposite case when A is in segment [C; B] (C is less or equals to B) we have both that multipliers are negative. If former comparison is negative - then number A is not in any of segments [B; C] or [C; B]. And here is a code of function on C/C++: int medianOfThreeNums(int A, int B, int C){ if ((A - B) * (C - A) >= 0) { return A; } else if ((B - A) * (C - B) >= 0) { return B; } else { return C; } } Show More Responses if (B-A) > 0 if (C-B) then B else C else if (C-A) > 0 then A else C a=b=c ? First get two numbers: x = a - b y = a - c now there are four possible cases for x and y if x & y are both positive => a is bigger than both b and c.=> choose bigger of b & c if x & y are both negative => a is smaller than b and c => choose smaller of b & c if x is positive and y is negative => a is bigger than b but smaller than c => choose a if x is negative and y is positive => a is smaller than b but bigger than c => choose a void GetMedian(int a, int b, int c) { int small, large; if (a < b) { small = a; large = b;} else {small = b; large = a;} // Check where c lies: if (large < c) return large; else if (c < small) return small; else return c; } @Moy: if the answer turns out to be small, then haven't you done 3 compares? Running Vitalii's code with a = 1, b = 7, and c = 3 produces a median == 7, which is incorrect. Suggestions? Vitalii's solution works for me. def find_median(a, b, c): ab = b - a bc = c - b if -ab * (ab + bc) >= 0: return a if ab * bc >= 0: return b return c

Apr 24, 2011
 Given a base 10 number, print the hexidecimal (base 16) representation of that number. 7 Answers public void printHex(int d) { string s = ""; int m = 0; int next = d; int a[16]; a[10] = "a"; a[11] = "b"; a[12] = "c"; a[13] = "d"; a[14] = "e"; a[15] = "f"; while (next > 15) { m = next % 16; if (m > 9 && m < 16) s = a[m] + s; else s = m + s; next = next / 16; } system.out.println(s); } B's answer doesn't work. I think a quick fix (besides all of the issues like incorrect array initialization, setting int to string, etc) would be to change while loop to a do while and the conditional to next > 0. Here is a generic solution passing radix which also handles negative numbers: public static String intToStr(int val, int radix) { char[] digits = new char[36]; for (int i = 0; i < 10; i++) { digits[i] = (char)('0' + i); } for (int i = 10; i < 36; i++) { digits[i] = (char)('A' + i); } String result = ""; boolean negative = false; if (val < 0) { negative = true; } val = Math.abs(val); do { result = digits[val%radix] + result; val = val/radix; } while (val != 0); if (negative) { result = "-" + result; } return result; } String buffer = new StringBuffer(); while (n > 0) { int base = n% 16; buffer.append((base<10 ? base : (Char)('a'+base-10)); n /= 16; } String result = buffer.toString().reverse(); Show More Responses lol, I wonder if this is acceptable printf ("%x",n); //n being the base 10 number To any base. private static String toBase(int number, int base, boolean literal){ String baseResult=""; while(number > 0){ int n = number % base; if(base == 16 && n > 9 && n < 16){ baseResult += getBase16Char(n); }else{ baseResult =n + baseResult; } number /=base; } return literal ? baseToLiteral(base).concat(baseResult) : baseResult; } private static String baseToLiteral(int base){ switch(base){ case 2: return "b"; case 8: return "o"; case 10: return "d"; case 16: return "x"; default: return "("+base+")"; } } private static char getBase16Char(int digit){ char[] base16char = new char[16]; base16char[10] ='a'; base16char[11] = 'b'; base16char[12] = 'c'; base16char[13] = 'd'; base16char[14] = 'e'; base16char[15] = 'f'; return base16char[digit]; } To any base. private static String anyToBase(int number, int base, boolean literal){ String baseResult=""; while(number > 0){ int n = number % base; if(base == 16 && n > 9 && n < 16){ baseResult = getBase16Char(n) + baseResult; }else{ baseResult =n + baseResult; } number /=base; } return literal ? baseToLiteral(base).concat(baseResult) : baseResult; } private static String baseToLiteral(int base){ switch(base){ case 2: return "0b"; case 8: return "0o"; case 10: return "0d"; case 16: return "0x"; default: return "0("+base+")"; } } private static char getBase16Char(int digit){ char[] base16char = new char[16]; base16char[10] ='a'; base16char[11] = 'b'; base16char[12] = 'c'; base16char[13] = 'd'; base16char[14] = 'e'; base16char[15] = 'f'; return base16char[digit]; } public String toHex(int num) { if(num == 0) return "0"; char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; String result = ""; while(num != 0){ System.out.println((num & 15)+ " "+ result); result = map[(num & 15)] + result; num = (num >>> 4); } return result.toString(); }

Apr 11, 2012

May 31, 2009
 Write a function that takes in an integer and returns the number of ones set in the binary representation. 4 Answers count = 0 while(num) { num &= num-1; count++; } return count; I thought the question was asking how to convert the integer to binary code and output it. function getOnes(\$int) { return substr_count(decbin(\$int), '1'); } Show More Responses __builtin_popcount(num) :D

Oct 6, 2012
 If you had a savings account with \$1, at a 100% interest rate, at what year would you have 15 billion dollars? 4 Answers Use the power of 2's to get to the answer. Log base 2 of 15 billion You should know offhand that 2^10 is 1024, so 2^20 = 1024^2 is approx a million, 2^30 is approx a billion, then you need four more years to get to 16 billion. Remember that you started with 1, not 2, so the answer is 35 years, not 34. Show More Responses After 34 years you would have 16 billion. That is to say, you would have 15 billion in the 34th year.

Nov 25, 2013
 None I could think of, I prepared everything from careercup yahoo questions and programming interviews book. I dint had an offer during the time I was writing this. 1. find right cousin of a given node. 2. simple oop and data structure questions. and lots of simple questions checking my ability to handle things in log(n) complexity. 3. binary search - took me into situation I wrote code in 3 diff, prog. lang's 3 Answers Did you finally hear from them? I too am waiting for a response from 2 weeks. I did try to call them around 50 times may be and 20 emails..finally the answer was NO. But just might be common sense thing, if the recruiter hasnt contacted, then the candidate was rejected. I hope I dint even believe my common sense, and dragged the situation. However the situation will not always be same for everyone. Good luck Samir, Hope you have a chance at Yahoo Thanks Vakshu. After seeing your response, I called up the HR (already sent a couple of emails). She says "I was good at the interviews. But the position was filled with another candidate. And they are finding another position for me." God knows. Whats the matter. If the position was already filled, why did they call me then? They should really stop playing with the sentiments of candidates. All of us prepare so hard to finally know that we are being turned down. I am not saying rejecting a candidate is hurting him/her. But delaying the result certainly hurts. I am no more hopeful !!!

Oct 23, 2012
 Isomorphic trees 3 Answers (print a tree, level by level) public static void printTree(Tree t) { int treeDepth = findDepth(t); for (int i = 0; i 0) { printLevel(t.left, level-1); printLevel(t.right, level-1); } } (a^3 + b^3 = c^3 + d^3) public static void a3b3c3d3() { Hashtable> h = new Hashtable>(); for (int a = 0; a prev = h.get(lhs); if (null == prev) prev = new ArrayList(); for (String s : prev) { System.out.println(a + " " + b + " = " + s); } prev.add(a + " " + b); h.put(lhs, prev); } } } (profit maximization) partition prices into non-decreasing sequences; buy at start of sequence of each sequence and sell at end; can be done iteratively by peeking ahead to next day's closing price