Software Engineer Intern Interview Questions | Glassdoor

# Software Engineer Intern Interview Questions

1,050

Software engineer intern interview questions shared by candidates

## Top Interview Questions

Sort: Relevance Popular Date

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

Jan 6, 2011
 Write a program to find the square root of a double. 5 Answers use binary search to narrow down the search space and keep multiplying the answer in each iteration by itself to check if it is equal to the double given. If it is lesser, move up the lower bound, else move down the upper bound. One easy to remember method that also has much better performance than binary searching for the result is the Babylonian Method. It is similar to Newton's method for finding derivatives. See the algorithm here: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method Also, combining this algorithm with the Rough Estimation also described on that page will compute the squareroot of most double numbers in less than 10 iterations. Is it too obvious to ask if you can do double^.5 ? Show More Responses I would respond by showing that I am thinking about the problem as it is defined, not by making assumtions about what is implied. This can result in product that does not meet the requirement specifications, which can be very costly: "What do you mean, a program or a function? A program would require input and output mechanisms to make it meaningful, but makes it pretty usesless as a job assessment question. A function makes more sense in this context. " There's a variation of the problem which says "find the square root of a double up to the third decimal digit". I'd just multiply the original number by 10^position (meaning, for 3 digit accuracy it'd be 10^3) and ran a simple loop to find the closest integer. As soon as i*i > 10^position * number, you can return (i-1)/10^position. It's hella slow and unimaginative, but it works and you won't sound like you knew this problem ahead of time (if you come up with a Babylonian solution).

Jan 6, 2011

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

Apr 25, 2012
 n= 20 for (i=0;i

Nov 8, 2010

Apr 16, 2012

### Software Engineering Intern at Palantir Technologies was asked...

Apr 16, 2012
 Say you have a single-column table of entries of variable size. Implement this table to also contain methods to lengthen one cell, cut a cell shorter, and to return which cell we're pointing at if given a certain distance from the beginning of the table. All methods need to be fast (assume a single-column table with many many entries). 6 Answers One way to do this is to use the doubly linked list. Assuming lengthening and cutting short a cell happens only at the end of the table, then both operations can be done easily and quickly on a D.L.List. Not sure what the interviewer meant by distance from beginning of the table... like number of bytes from the beginning of the table and he wants the cell number? For example, if each cell has length 5, and the user clicks on 13 from the top, then you need to let the user know he has clicked on cell 3. This operation needs to be fast. The search function needs to be doable in less than linear time, and so do inserts and deletes Show More Responses binary indexed trees link list of link lists An dynamically allocated array that can be resized..

Oct 23, 2012
 Write a function in language of your choice that takes in two strings, and returns true if they match. Constraints are as follows: String 1, the text to match to, will be alphabets and digits. String 2, the pattern, will be alphabets, digits, '.' and '*'. '.' means either alphabet or digit will be considered as a "match". "*" means the previous character is repeat 0 or more # of times. For example: Text: Facebook Pattern: F.cebo*k returns true 8 Answers NOTE : I didn't really get the part of : "*" means the previous character is repeat 0 or more # of times. If it can be repeated 0 or more times, that means it's always true... I might have misunderstood this part. This is the code considering '*' and '.' are exactly the same. bool myStringCompare(char* string1, char* string2){ bool match=false; for(int i=0; string1[i] != '\0' && string2[i]!='\0'; i++) { bool condition1 = string1[i]==string2[i]; bool condition2 = (string1[i]=='*' || string1[i]=='.') && (isalpha((int)string2[i]) || isdigit((int)string2[i])); bool condition3 = (string2[i]=='*' || string2[i]=='.') && (isalpha((int)string1[i]) || isdigit((int)string1[i])); if(condition1 || condition2 || condition3) match=true; else{ match=false; break; } } return match; } I have used '#' instead of '*' . this code goes according to the rule that '#' gives 0 or more number of prev character value. #include #include #include bool ifMatch(std::string text, std::string pattern) { int tlen = text.length(); int plen = pattern.length(); std::string output = ""; char prev = '\0'; int k = 0; for(int i=0; i< plen && k < tlen; i++) { char textc = text[k]; char patternc = pattern[i]; if(patternc == '#') { if(prev == '\0') prev = '#'; else if(prev == text[k]) { output = output + prev; k++; } } else if(patternc == '.') { prev = textc; output = output + text[k]; k++; } else if(textc == patternc) { output = output + textc; prev = textc; k++; } else prev = pattern[i]; } std::cout << output << std::endl; if(strcmp(text.c_str(),output.c_str())==0) return true; return false; } int main() { ifMatch("facebook","#m#f.cn#bo#k"); } #include using namespace std; bool regex_match(string s1, string s2); int main() { if(regex_match("facebook", ".*.")) { cout << "Pattern Matched!" << endl; } else { cout << "Pattern Not Matched!" << endl; } return 0; } bool regex_match(string s1, string s2) { char c1, c2; int s2i = 0; for(int i = 0; i < s1.length(); i++) { c1 = s1[i]; c2 = s2[s2i]; if(c2 == '.') { s2i++; continue; } if(c2 == '*') { c2 = s2[s2i - 1]; bool done = true; c1 = s1[i - 1]; for(int j = i; j < s1.length(); j++) { c1 = s1[j]; if(c2 != '.' && c1 != c2) { done = false; i = j - 1; break; } } if(done) { break; } } else if(c1 != c2) { return false; } s2i++; } if(s2i < s2.length()) { for(int j = s2i + 1; j < s2.length(); j++) { if(s2[j] != '*') { return false; } } } else { return true; } return true; } Show More Responses #include #include using namespace std; bool regex_match(string s1, string s2); int main() { string strs[10] = {"facebook", "f.cebo*k", "*", ".*", "facebo.*", "facebo.*k", ".*.", "", " ", ".....o*."}; for(int i = 0; i q; for(int i = 0; i < s1.length(); i++) { q.push(s1[i]); } char last_char; for(int i = 0; i < s2.length(); i++) { if(q.empty()) { return false; } char c1 = q.front(); q.pop(); if(last_char == '\0') { last_char = c1; } if(c1 == '.') { last_char = c1; continue; } if(c1 == '*') { if(last_char == '.' || last_char == '*') { last_char = c1; break; } bool done = true; for(int j = i; j < s2.length(); j++) { if(s2[j] != last_char) { done = false; i = j - 1; break; } } if(done) { last_char = c1; break; } } else if(c1 != s2[i]) { return false; } last_char = c1; } if(!q.empty()) { while(!q.empty()) { if(q.front() != '*') { return false; } q.pop(); } } return true; } boolean accepts(char* pattern, char* s) { if (!pattern || !s) return 0; if (0 == *pattern) return (0 == *s); if ((strlen(pattern) > 1) && (pattern[1] == '*')) { return (match(pattern, s) && accepts(pattern, s+1)) || accepts(pattern+2,s); } else { return (match(pattern, s) && accepts(pattern+1, s+1)); } } boolean match(char* pattern, char* s) { return ((*pattern == '*') || (*pattern == *s)); } @Rahul: a small bug there (matching the '*' rather than the '.'): boolean match(char* pattern, char* s) { return ((*pattern == '*') || (*pattern == *s)); } should be: boolean match(char* pattern, char* s) { return ((*pattern == '.') || (*pattern == *s)); } Other thoughts: -- "if ((strlen(pattern) > 1)" is redundant since "if (!pattern[1] =='*')" takes care of it. (and I'd generally avoid a strlen in either a loop or recursive call) -- This solution ends up with a stack depth that is equal to the length of the target string -- far from ideal. #include using namespace std; bool match(string s1, string s2) { bool matching = true; char c1 = s1.at(0), c2 = s2.at(0); unsigned int i = 0, j = 0; while(matching && (i 0 && c1 == s1.at(i - 1)) { i++; } else if(c2 != c1) { matching = false; } i++; j++; } return matching; } int main() { bool m = match("faceboooooook", "f.cebo*.k"); if(m) { cout<<"matching"<

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 Answers I 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 Responses bool 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 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 Answers Isn'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
2130 of 1,050 Interview Questions

More