Software engineer new grad Interview Questions | Glassdoor

# Software engineer new grad Interview Questions

790

software engineer new grad interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

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?11 AnswersPick 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 Responsesif (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 avoid 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 ccan't you sort the 3 numbers, the median is the middle one...?

Jun 16, 2020
 Improve this piece of code, loop tracing, very basic printing problem10 AnswersDid you get to choose which programming language?I guess, what happened is that I told them my strongest language was Java and they said you can write it in that language if you want. The "improve this piece of code" portion was in python, the loop tracing was in Java I believe, and the printing problem was open ended. But if you have programming experience you should be able to tell what the code does pretty easily, it's definitely not hard. Just know they might reject you regardless of if you do well. Maybe it's just the Covid-19 situation.Did they ask salary expectations? Do you think you might have been rejected because you asked for too much \$? And you say you answered everything correctly you think?Show More ResponsesWe didn't even get to salary expectations actually. Yea I'm very confident I answered everything correctly. It was definitely not a difficult assessment at all. I know it's kinda tough but try not to be discouraged, unfortunately it's not uncommon for you to ace a technical but still get rejected. All we did was have the technical thing on a google doc, then afterwards they explained what the company does, which is build custom software essentially, and then asked if I had questions. Hopefully you guys have better luck than I did.I have an interview with them this upcoming next week, I am sorry it did not work out I am sure you did great. Makes me a bit nervous you aced it and didnt get a call back.Appreciate it, hopefully you have better luck than I did. Don't give up regardlessAny SQL questions? Like anything more complex than just basic JOINS? Also what exactly does "improve this piece of code" mean if you are willing to share? How would one prepare for that?That would be awesome to have some insight currently I am trying to grind leetcode to prepare but unsure if that is a good method,Got a rejection email from someone who did not even interview me stating they appreciated the chance to learn more about me. Very odd, the questions were simple and not difficult , first one was a loop tracing , second was fizzbuzz question and third was improve 2 for loops and you could write out the code or write a paragraph on how you would fix it.Ok, for starters I didn't get any SQL questions at all. The person who commented above me got the same interview questions as me. The improve this code one was me given a nested loop(so n^2 runtime), and then they asked me how I would go about improving this code's runtime. The given code is a brute force solution so you'll probably be fine. Hopefully that gives more detail about the question. As for leetcode/other stuff, I don't think that really helps tbh. I mean, these questions were not difficult by any stretch. The fact that you even know what leetcode is and are probably using it tells me you probably have enough skill to get the questions right. Unfortunately, it's pretty common to ace a technical and still get rejected. Just try to keep your head up, these are tough times.

Apr 24, 2011
 Given a base 10 number, print the hexidecimal (base 16) representation of that number. 7 Answerspublic void printHex(int d) { string s = ""; int m = 0; int next = d; int a; a = "a"; a = "b"; a = "c"; a = "d"; a = "e"; a = "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; 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 Responseslol, I wonder if this is acceptable printf ("%x",n); //n being the base 10 numberTo 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; base16char ='a'; base16char = 'b'; base16char = 'c'; base16char = 'd'; base16char = 'e'; base16char = '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; base16char ='a'; base16char = 'b'; base16char = 'c'; base16char = 'd'; base16char = 'e'; base16char = '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
 We have a fairly large log file, about 5GB. Each line of the log file contains an url which a user has visited on our site. We want to figure out what's the most popular 100 urls visited by our users.6 AnswersPossible Solution: not necessary the right one. Notes: Will recommend use non recursive version of quick sort since 5GB log file could be considered to generate URL data to 500 MB ball park and worst estimate. That would blow the below solution. Also keeping the file open all the time while reading rather than putting it in buffer, since 5GB is a large file size and that may not hold in the memory provided for the program. Will have to ensure that file remains untouched in those moments. # Program to find the URLS that have been most referenced and used from a log file. import sys # Function that will quicksort the list of tuples that have occurence number as first # element in tuple and the URL as the second element in the list. The list is sorted # from the least referenced to most referenced as done in classical quicksort. # From: www.algolist.net/Algorithms/Sorting/Quicksort def sortDict(impurllist, left, right): i, j = left, right - 1 pivot = impurllist[int((left + right) / 2)] while (i pivot): j = j - 1 if (i len(urllist): for element in reversed(urllist): print(str(element), '\t', str(element)) else: j = -1 for i in range(num): print(str(urllist[j]), '\t', str(urllist[j])) j = j - 1 # Definition of main. def main(): try: int(sys.argv) except ValueError: print('Please input a integer for the number of URLs you want to view.') return # Terminate program execution if the arguments provided are not right. if len(sys.argv) != 3: print('Usage: python3.2 HighURLFinder.py #URLs_you_want Log_file') return else: logdict = readLogFile(sys.argv) if logdict is None: return # Terminate execution since the file read program encountered error. else: impurllist = sortLogDict(logdict) showUrlLast(impurllist, int(sys.argv)) # The starting point of the program main()Quick Sort is O ( n log n ) avg case, so we could do better O (n) Now, onto design : Do the lines have other information other than URL? If so, we need to filter them out using Regular Expressions. Possible O (n) solution: A hashmap with URL's as keys and number of hits as values would yield a O(n) run-time to populate it. Algo: while input from file getURL (or filter out URLs from line) if URL exists in hashmap, increment its count else add URL to hashmap Ofcourse, being a large data set, we might get a ton of random disk seeks due to paging of memory. After profiling, if we really find that to be a problem then we could do something like encode the urls into something smaller that takes less space and hence has a lesser probability of causing paging. I can think of few more solutions but I think this should be sufficient.I would add a min-heap of size 100 to BetterSolution's answer (in order to get the top 100).Show More Responsescat log | sort | uniq -c | sort -k2n | head 100Well, I believe, we can create a HashMap with as the key and visit times as Value. Once we get all the values(i.e. URL visited), we can do a Min Heap Sort. Its like saying I have n number in an array and I need to find top k number (n>>k) ,So applying Min Heap would cost O(nlog k), whereas if we use the best sorting technique, we get O(n log n). But Hey @ David Response is a smart one in relevance to FrontEnd Engg position.If you don't need 100% accuracy, which in this case you probably don't, you can use a Count-Min-Sketch data structure to come up with a pretty accurate estimate ( with some acceptable degree of error ) in a streaming fashion. Very little memory needs to be used in this approach.

Mar 12, 2013
 He asked me to write a function to detect whether string1 contains all letters in string24 Answers// Assume S1 and S2 are non-null pointers bool hasChar(char *s1, char *s2) { int ret=false; char *tmp; if (*s2 == '\0') return true; tmp = s1; while ((*tmp != '\0') && (*tmp != *s2)) tmp++ if (*tmp == '\0') return false; return hasChar(s1, s2++); }Traverse string2 and create a map where the characters of string2 are used as keys. Then traverse string1. If the the character in string1 exists in the map, remove the key from the map. At the end if your map has anything in it, string1 did not contain all of the characters.private static bool isEqual(string s1, string s2) { int[] first = new int; int[] second = new int; for (int i = 0; i 0) return false; } return true; }Show More Responsesdef hasAllLetters(str1,str2): if str2 == "": return True else: dict = {} for ch2 in str2: if ch2 in dict: dict[ch2] += 1 else: dict[ch2] = 1 for ch1 in str1: if ch1 in dict: if dict[ch1] > 0: dict[ch1] -= 1 else: del dict[ch1] else: return False return True

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 AnswersUse the power of 2's to get to the answer.Log base 2 of 15 billionYou 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 ResponsesAfter 34 years you would have 16 billion. That is to say, you would have 15 billion in the 34th year.

Dec 1, 2018
 Given a bug report of a common Python library (everyone would know this library), run tests to observe the issue and then fix it.5 AnswersI was not used to this kind of question so I did not complete the taskJust curious, what common Python library is it? And what kind of issue do you have to fix?Thus, what's the library....Show More ResponsesI can't share the library because it wouldn't be fair but it is a common Python library. Besides that, the library you get could be different to mine. The bug was something related to an inconsistent behaviour of the program. One or more comments have been removed. Please see our Community Guidelines or Terms of Service for more information.

May 31, 2009
 Write a function that takes in an integer and returns the number of ones set in the binary representation. 5 Answerscount = 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 One or more comments have been removed. Please see our Community Guidelines or Terms of Service for more information.