Engineering Intern Interview Questions | Glassdoor

# Engineering Intern Interview Questions

1,935

Engineering intern interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

Sep 26, 2012

### Software Development Engineer Intern at Amazon was asked...

Jan 30, 2012
 Write a program that sees if two binary trees are equal.6 AnswersI utilized a depth-first traversal method, comparing the data within each node and recursively checking the left, then right child.Don't know if this works or not... value = \$value; \$this->left = \$left; \$this->right = \$right; } // O(n) times inorder traversal function testEsqual(\$tree1,\$tree2) { if(\$tree1->value ==null || \$tree2->value==null) return false; if(\$tree1->value ==null && \$tree2->value==null) return true; while(\$tree1->value!=null) { if(\$tree1->value == \$tree2->value) { equal(\$tree1->left,\$tree2->left); equal(\$tree1->right,\$tree2->right); } else { return false; } } } } ?>How if we use in-order traversal? If my understanding is correct, two binary trees are equal if they contain the same elements (although at different positions in the tree)Show More Responsesbool AreEqual(Node* node1, Node* node2) { if( (node1 == NULL && node2 != NULL) || (node2 == NULL && node1 != NULL ) return false; if(node1 == NULL && node2 == NULL) return true; if(node1->data != node2->data) return false; return( AreEqual(node1->left, node2->left) && AreEqual( node1->right, node2->right) }; int main(); { AreEqual(root1, root); };The solution by kvr doesn't cover a case when node1->data and node2->data are equal. An additional if is required.fb, If if(node1->data != node2->data) is not true, what does that tell you *Is* true?

### Software Development Engineer Intern at Amazon was asked...

Feb 10, 2013

Dec 13, 2013
 Given a string write a function which prints all the subsets of the string. Now make the function to return only unique solutions. For example if they give you "abc" you print out a ab abc ac b bc c Now for the unique solution constraint, if they give you "aba" the output should be: a ab aba b7 AnswersThere is a divide & conquer strategy with this problem. Divide string into two halves. Recursively print the string on left and then on right. Finally, for each string on left, combine it with every string on the right. (Note this will not give the unique solution.) To obtain the unique solution, a simple way is to use a set to keep track of every substrings we have constructed. If element is already in the set, we simply not add it. In the end, we print out every element in the set. Maybe there is a better way without using extra memory.What about counting the number of each character in a string? for "aba" we have a-2, b - 1. now recursive solution which takes charecter, for a we choose 0a, 1a, and 2a and push to the 'b' where take 0b or 1b. now we have "", "a","aa","ab","b". all subsets + empty one.@Allen: The interviewer specified that he wants a recursive solution and you are not allowed to use extra memory. Note: The interviewer gave me a big hint: "What happens if you sort the array and simply generate all the subsets?"Show More Responses@Marius: what do you mean by "sort the array"? There is no array in the questionSee this link and view discuss session for solution to unique subsets https://oj.leetcode.com/problems/subsets-ii/So this requires subsets to be printed and not substrings; ordering of characters doesn't matter. So sort the characters and generate unique subsets. import java.util.Arrays; import java.util.Scanner; public class UniqueSubsetGenerator { public static void main(String[] args) { final Scanner in = new Scanner(System.in); while (true) { System.out.println("Enter string : "); final String masterWord = in.nextLine(); char[] word = masterWord.toCharArray(); Arrays.sort(word); generateUniqueSubsets(word, 0, ""); } } private static void generateUniqueSubsets(char[] word, int start, String prefix) { //System.out.println("start " + start + " prefix " + prefix); if (start >= word.length) { System.out.println(prefix); return; } char ch = word[start]; int count = 1; for (int i=start+1; i= len(inpStr): return charLen = getLen(index, inpStr) for i in range(charLen + 1): posSet = currSet + inpStr[index] * i uniqueSubsetsHelper(subSets, posSet, index + charLen, inpStr) def getLen(index, inpStr): currChar = inpStr[index] currLen = 0 while index < len(inpStr): if inpStr[index] == currChar: currLen += 1 else: return currLen index += 1 return currLen

### Software Engineer Intern at eBay was asked...

May 17, 2012
 Assuming a preexisting list of 100 words, how would you efficiently see if a word received from input is an anagram of any of the 100 words?4 AnswersFirst Sort 100 words and keep the hash of sorted words.. Now when you recieve a word, SOrt it and check if Hash contains that key. O(nlogn) Where n is length of String.Hi I think you can take a look at the most efficient algorithm for this question in this link www.crackeasily.com/2012/01/find-whether-2-strings-are-anagrams.htmlTo say what MW said more clearly: If you sort words that are the same anagram they will always look the same. Example: "god" and "dog" both look like "dgo". So you sort the input word, then you iterate through the list of 100 words, and while looking at each word sort that word. compare if the input and that word are the same. If they are then you have found an anagram. if you made it to the end of the list and you haven't found an anagram then an anagram does not exist in the list.Show More ResponsesSorting takes O(nLogn) average time. This can be further improved by counting characters in the strings. So, for the given string calculate count of each character in an array [256]. For each word in the list, check if it contains the same count for its characters and return that word. Complexity O(n)

### Software Engineering Intern at Yelp was asked...

Mar 7, 2013
 You have two arrays with N integers in them. Merge those arrays using a recursive algorithm so that the integers in the final array are sorted.6 AnswersIsn't this essentially mergesort?how to solve it?def funky(arrOne, arrTwo, arrThree, indexOne, indexTwo, indexThree): if(indexOne >= len(arrOne) and indexTwo >= len(arrTwo)): return arrThree if(not(arrOne[indexOne] in arrTwo)): arrThree.append(arrOne[indexOne]) if(not(arrTwo[indexOne] in arrOne)): arrThree.append(arrTwo[indexTwo]) indexOne = indexOne + 1 indexTwo = indexTwo + 1 funky(arrOne, arrTwo, arrThree, indexOne, indexTwo, indexThree)Show More Responses@Dung Pham you are right it is mergesort def merge(a1, a2): """ it is mergesort with unusual interface 2x Array on input """ return _merge(a1 + a2) def _merge(seq): if len(seq) = a_size: out.extend(b[b_i:]) break else: out.append(b[b_i]) b_i += 1 if b_i >= b_size: out.extend(a[a_i:]) break return out if __name__ == "__main__": got = merge([1,10,2,4], [12,1,9,7]) print(got)Actually, merge sort will require an extra O(m + n) space to store the temporary merged array. Probably it's better to concatenate two arrays then do quick sort?def merge(nums_1, nums_2): result = [] if len(nums_1) == 0: result.extend(nums_2) return result if len(nums_2) == 0: result.extend(nums_1) return result if nums_1[0] < nums_2[0]: result.append(nums_1[0]) result.extend(merge(nums_1[1:], nums_2)) else: result.append(nums_2[0]) result.extend(merge(nums_1, nums_2[1:])) return result

Jul 5, 2012

Oct 18, 2011
 How can one implement a queue with only a stack implementation?4 AnswersYou will need at least two stacks. public class Queue { private Stack inbox = new Stack(); private Stack outbox = new Stack(); public void queue(E item) { inbox.push(item); } public E dequeue() { if (outbox.isEmpty()) { while (!inbox.isEmpty()) { outbox.push(inbox.pop()); } } return outbox.pop(); } }you have double ended queue (Deque) and insertion and removal can be handled at both end either left or right. But if you want to implement Queue as stack, then restrict the operation at any one end either left. So Front and rear both become at the same end.Never mind my previous answer was wrong implementation. It is actually implementing stack using Queue.Show More Responses#include #include using namespace std; stack st1, st2; int pop( ) { if (!st1.empty()) { int t,t1; while(st1.size() != 1) { t = st1.top(); st1.pop(); st2.push(t); } t1 = st1.top(); st1.pop(); while(st2.size()) { t = st2.top(); st2.pop(); st1.push(t); } return t1; } } int main() { st1.push(2); st1.push(3); st1.push(4); st1.push(5); st1.push(6); cout<

### Software Engineer Intern at Amazon was asked...

Mar 20, 2012
 Implement integer division4 Answerspublic class Solution { public void integerDivision(int num, int den){ int q=0,rem=0; while(num>=den){ num-=den; q++; } rem=num; System.out.println("Quotient "+q+" Remainder "+rem); } public static void main(String[] args) { Solution sol = new Solution(); sol.integerDivision(73, 9); } }public static int division(int a, int b) { if (a b) { int q = 1; int c = b; while ((b = b > 1; while ((a = a - b) >= c) { q++; } return q; } else { return 0; } }Minor correction public static int division(int a, int b) { if (a b) { int q = 1; int c = b; while ((b = b > 1; while ((a = a - b) >= c) { q++; } return q; } else { return 0; } }Show More ResponsesI hope this will be helpful int divFunc(int* num,int* den,int* rem) { int q = 1; int backupNum = *num; int backupDen = *den; while((backupDen = backupDen >=1; backupNum = backupNum - backupDen; *num = backupNum; *rem = backupNum; return q; } void divide(num,den) { int q=0,r=0; while(num>den) { q = q + divFunc(&num,&den,&r); } printf("Q = %d\t R = %d\n",q,r); }