Software Development Engineer Interview Questions | Glassdoor

# Software Development Engineer Interview Questions

4,121

Software development engineer interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

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

Sep 1, 2011
 Using only putchar how would you print out the ascii values for each digit in an integer. For example if the integer was 123, then you would want to print the ascii values for 1, 2, and 3.5 AnswersI used a recursive method involving modulus and division by 10. Not hard, just stressful writing it on paper in an interview.void printAscii( char *input) { while(input) { putchar(*input++); } } This should work as well....printing each char as an integer would give its ascii valueAh...the above code would just print the numbers itself not ascii values Could be fixed by adding ascii value of 0 char ie. putchar(*input + '0'); input++;Show More Responsesvoid printASCII(int src) if (src > 9) { printASCII(src/10); } putchar('0' + (src % 10)); }void print_ascii(int n) { while(n) { int k = (n%10)+'0'; n/=10; putchar(k); printf("\n"); } }

### Software Development Engineer at Microsoft was asked...

Jan 4, 2010
 Write an algorithm to verify if a tree is a binary search tree. 5 AnswersBST(Node * p) { if (p==null) return false; if(p->left! null& p->dataleft) return false if(p->right&&p->data>=max(p->right)) return false if(!BST(p->left) && BST(p->right)) return true return false; }This solution is wrong. if (p==null) return false; - Why is this false?isBST(node n) if(n.left == null || n.right == null) return true; if(n.left.val < n.right.val) return isBST(n.left) && isBST(n.right) else return false;Show More ResponsesThis solution is also false. It's going to return true if there is no right node and the left node is greater than the parent. It's basically right, and is a simple fix to change that, so I'll leave it to the reader as an exercise. :)Assume that we have a binary search tree like this: RootNode has value 6. RootNode->LeftChild has value 3. RootNode->LeftChild->LeftChild has value 2. RootNode->LeftChild->RightChild has value 7. We know that in a binary tree 1.all nodes' values which are at left side of a node must be less than Node's value. 2.all nodes' values which are at right side of a node must be greater than Node's value. In anonymous algorithm it does not check: 1. if node's left child's value is less than node's value && node's right child's value is greater than node's value 2. node's all left childrens' values must be less than node's value.

### 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 at Amazon was asked...

Oct 24, 2012
 Given an array, return the nth largest.4 AnswersCan be in any language. I did write the working code, but the interviewer pointed out that it won't work for some special cases.public static void sortArray(int n){ Integer[] arrayList = {12,2,5,1,7,8,3,4,9,10,13,11,6}; Arrays.sort(arrayList); System.out.println("for number : " + (n + 1) + " we get: " + arrayList[n]); }No sorting...it would take nlgn for sorting and it could be faster..Show More Responseshttp://en.wikipedia.org/wiki/Selection_algorithm

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

Apr 5, 2010
 Given two (huge) sets, what is an efficient way to find their intersection?4 Answershey can you please share the answer with us? I have 2 ans: 1. sort both the sets and iterate to find intersections. (nlogn) 2. use hash. (n) Please let me know if it could be done in a better way. Thanks!I think "huge" is an indication that using a hash set approach would be untenable, the idea being that you'd run out of RAM. The n log n approach seems correct to me.Dont u think nlogn is larger than n since in data structures, the base of the log is always 2Show More Responseshe meant untenable in space. in time of course you cannot beat the hash

Apr 12, 2012

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

Sep 7, 2010
 Find the longest subsequence in a given array of numbers in O(n)4 Answers//Given an array of numbers find the longest subsequence //Using hash_maps :: complexity O(n) #include #include #include #include #include #include using namespace __gnu_cxx; using namespace std; void itoa(int i,string *s){ std::stringstream out; out , eqstr> myHash; int main() { myHash array; int inputArr[20] = {1,43,4,5,6,17,12,163,15,16,7,18,19,20,122,124,125,126,128,100}; int seqSize = 0; vector longSeq; vector tempLongSeq; for(int i=0;ifirst);seqSize=1; ++it; int data; //Iterate through each element and check if it is the next in sequence for (; it != array.end(); ++it) { data = it->first; if(data==(tempLongSeq[tempLongSeq.size()-1]+1)){ tempLongSeq.push_back(data); seqSize++; }else{ //sequence ended if(seqSize>longSeq.size()){ //if the current sequence is longest then store it else longSeq.clear(); longSeq = tempLongSeq; } tempLongSeq.clear(); tempLongSeq.push_back(data); seqSize=1; } } cout<Hey guys... the above code needs changes. if values are greater than 193 than it should fail because of hash_map's bucket_count.. anyways.. use map instead of hash_map. which is sorted. but this gives a complexity of O(nlogn)How about: Traverse the array, have an initial index of start of sequence, and update the end index each time the next element is in sequence. If the sequence ends, check the difference between start/end, and if it is greatest so far than save those two indices and length of sequence.Show More ResponsesQuestion - Should only the successive sequence of numbers be taken into account or sequence of numbers formed from the numbers in any position?

Sep 26, 2012