Senior Software Engineer Interview Questions | Glassdoor.ca

# Senior Software Engineer Interview Questions

300

Senior software engineer interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

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

Aug. 16, 2012
 Write a function that divides one number by another, without using the division operator, and make it better than O(n).7 AnswersThis can be done in a recursive function, the following code is in Python. # get result of a/b without using a "divide" operator def div(a,b): if a < b: return 0 else: return div(a-b, b)+1 This is how human being do the division naturally, however, the running time of this is O(n/m), where n is the size of a, and m is the size of b, which means, O(n/m) is guaranteed to be less than O(n), when m is larger than 1. -MaximThe answer above is still O(n). We can use binary search and find the answer in the interval [1,a] and use multiplication operator.Totally agree with Vasil. Other option: Long Division Algorithm. O(log n) anyway.Show more responseswhy not just a * b^(-1) :-)// Write a function that divides one number by another, without using the division operator, // Assume that x%y = 0 // O(log n) (function() { 'use strict'; var divide = function(x, y) { var xLength = (x + '').length; var i = 0; var result = 0; var xAry = ('' + x).split(); var xStart = ''; for (i = 1; i = y) { xStart -= y; result = parseInt(result, 10) + 1; } } } return result; }; console.log(divide(1000, 4)); })();Use logarithms? O(1) log(x/y) = log(x) - log(y) = log(answer) answer = 10 ^ log(answer)Convert the number to divide into the base of the number that you are dividing with and then shift the 'bits' to the right by 1 then convert back to decimal

Dec. 2, 2013
 After asking the details of my current role, he only gave me a simple coding question. Write a function using C++ or Java that is passed an integer and it returns the number of bits set to 1. Is there a way to improve your solution and make it faster and more efficient?3 AnswersThere are obviously multiple solutions: Solution 1: Set sum = 0 Find the remainder by dividing by 2. Divide by 2 for the next iteration. Solution 2--much better. Set sum = 0 Start loop Set mask = 1 sum += mask & number Loop return sumThere is another way, that is explained here: http://en.wikipedia.org/wiki/Hamming_weigh (with just 24 operation and without any cycle you can find the number of bit set to 1)An example in Java with 10000 results in answer 5 int number = 10000; int numberOfBitsSet = 0; for (int i = 1; i <= number;) { int result = (i & number); if (result != 0) { numberOfBitsSet++; } i = i << 1; } System.out.println(numberOfBitsSet);

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

Aug. 11, 2011
 find LCA for two nodes of a binary tree. 4 AnswersList 1: tree nodes inorder List 2: tree nodes postorder List 3: all the nodes in between the given 2 nodes List 4: all the nodes after the given 2 nodes the LCA is the common node in List 3 and List 4.I think there's an easier solution: List1: the parents of node1 in order bottom to top (can get them by navigating up tree from node 1). Navigate up tree from node2. First parent of node 2 found in list1 is LCA.There is a better and standard approach. The key is to visualize the ancestor in a BST (Of course we are referring to a BST here). If you start from the top of the tree while comparing both values to each node, the point where the branching differs, is going to be your NCA I wrote sample code a while back too http://forum.codecall.net/topic/64400-finding-nearest-common-ancestor-in-a-bst/Show more responsesThe advantage? Well complexity is log (n) and there is no extra memory required i.e. no list creation needed

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

Aug. 11, 2011
 how to merge two sorted linklist? 2 AnswersUse MergeSort.You don't need merge sort here. This is a O(n) task, because the lists are already sorted. In fact, this merge routine is part of merge sort. No sorting takes place here, only merging, and it could be done in linear time. Node* merge(Node* list1, Node* list2) { Node* merged = null; Node** tail = &merged; while (list1 && list2) { if (list1->data data) { *tail = list1; list1 = list1->next; } else { *tail = list2; list2 = list2->next; } tail = &((*tail)->next); } *tail = list1 ? list1 : list2; return merged; }

Jun. 5, 2012
 So, you have almost talked to everyone in the building?1 AnswerPut me in a very unconformable situation.

### Sr. Software Engineer at Thomson Reuters was asked...

Jan. 11, 2018
 Hackerrank question: valid braces segregate even and odd numbers 1 Answersimple algorithm

### Senior Software Engineer at Synopsys was asked...

Aug. 17, 2019
 Write Fizz-Buzz in C++. Now make all the stupid constant-time optimizations you can think of to it. Formally: given an int, return “fizz” if it’s a multiple of 3, “buzz” if it’s a multiple of 5, or “fizz buzz” if it’s a multiple of both. It it isn’t a multiple of 3 or 5, return “”. Then find stupid ways to over-optimize it, such as using no if-statements.1 AnswerEveryone who knows what an of statement is has written fizz-buzz. Check x%3, then check x%5, and write out your four if/else cases. You can use only a single branch with a switch on from x%15 from -14 to 14, or no branches at all by hardcoding 30 strings in an array and accessing that index.

### Senior Software Engineer at Synopsys was asked...

Aug. 17, 2019
 Given an int, return it as a string without using any library methods to do so. It should work for all values of an integer.1 AnswerPretty common question everyone should have learned in high school, except this one is only for base 10, so it is even easier. I made sure I handled 0 and -2^31 correctly. -2^31 I special-cases out, but in hindsight a cleaner solution would be to just store x as a long long before doing anything. You need to watch out for -2^31 because if you multiply it by -1, you get the same thing.

### Senior Software Engineer at Synopsys was asked...

Aug. 17, 2019
 Find the height of a binary tree.1 AnswerLiterally 2 lines: If (!node) return null; return 1+max(height(node->left), height(node->right));

### Senior Software Engineer at Synopsys was asked...

Aug. 17, 2019
 Reverse a linked list. Return the k’th last node of a linked list, or NULL if it doesn’t exist.1 AnswerWalk through it and flip each edge, then set the edge from the old head to NULL. For the follow-up: Walk two pointers through the list until the second one hits the end. Then your first pointer is the answer. Disappointingly boring interview that ended early because he had no more questions.
110 of 300 Interview Questions