Senior Engineer Interview Questions | Glassdoor

# Senior Engineer Interview Questions

8,333

Senior engineer interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

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

Mar 19, 2009
 Considering a 2-dimension matrix that can only be traversed by 1 adjacent position at a time and never diagonally. Create an algorithm to traverse that matrix from its upper-left corner to its lower-right corner using the shorter possible path in the most efficient way.5 AnswersThis is a very interesting problem. Although I knew immediately that I had to use recursion to effectively traverse the matrix and eventually got a working algorithm, the catch to make the algorithm much more efficient is to traverse the matrix backwards.How back traverse make the algo efficient?Why not just go all the way down then all the way right? Without diagonal moves the path length is fixed. Unless they provide a different definition for 'shortest length'Show More ResponsesFor a thinking outside the box answer.... assuming the set is closed for indexing, go up -1 and left -1.I guess it's the traversal of a graph's depth first search (DFS) using Adjascent matrix...

Mar 11, 2012
 Find the largest sum of contiguous numbers in an array 6 AnswersAre you sure?! If this is the question then just add all numbers of the array lol.What about negative numbers in the array - you can't just sum it upGood point! In that case, just iterate over the array and: 1) n 0, add 3) next time you encounter n <=0, compare with the current max number and assign the new sum if larger.Show More ResponsesThe key word is "contiguous" - the solution above will nto work. Just google the solution - there are quite few options here.maximum subarray algorithm http://en.wikipedia.org/wiki/Maximum_subarray_problemDynamic programming

May 5, 2009
 If you want to distribute a large file (gigabytes) in a large (100+ machines) park how do you do it?5 AnswersFirst I mentioned rsync, then shell scripts with lists of machines that copy the file via FTP or SFTP. Also dividing the network into a tree of machines that copy their file on to child-nodes would be possible. You could also split the files into smaller pieces and speed up the process this way. The problem may be nodes that are down or slow. Finally you could send the small pieces to random nodes until the big file has arrived everywhere.Bittorrent! Or via some gossip algoSince you have control over these machines and their network, you can impose the requirement of multicast support. Multicast file distribution is not uncommon. There will be some overhead for retries, but it should otherwise scale well.Show More ResponsesYou distribute the file in tree-like fashion and keep the total number of copying tasks at any moment under a threshold because otherwise you slow down the network to a halting point.Assuming you did not reserve a VLAN for the purpose of isolating your regular activities.. I would schedule the whole process at a good time (middle of night?). Use multicast if possible. And pace the data rate to a minimum. But, why the hack didn't you isolate your regular internal network traffic from service update traffic using VLAN?

May 5, 2009
 Look for a string in a very long string - a needle in a haystack. Write the program in pseudo-code.5 Answersfunction isSubstring (needle, haystack) { for(int i=0; i

Oct 13, 2010
 Find the least common root for 2 numbers in a BST5 AnswersNode FindCommonRoot(root,x,y) { if(!root) return NULL if((root->value value right,x,y) else if((root->value > x) && (root->value > y)) return FindCommonRoot(root->left,x,y) else if(((root->value > x) && (root->value value value > y))) return root }Node *Tree::LeastCommonRoot(x, y) { Node *runner = this->root; while (runner) if (x value && y value) runner = runner->left; else if (x > runner->value && y > runner->value) runner = runner->right; return runner; }Dear Lamont, You need another "else" statement which should return runner. Otherwise there will be an infinite loop in some cases (when one of x or y is equal to the current root).Show More ResponsesHere's a Java version: // This method is in BSTNode class BSTNode findLowestCommonAnsector(int va1, int val2) { BSTNode root = this; while(root != null) { int val =root.value; if(val > val1 && val > val2) root = root.left; else if(val < val1 && val < val2) root = root.right; else return root; } return null; }@Aytekin - D'oh! This code originally had an "else break" but I failed to type it in here. It's not like this is a special case. This is what happens when we find the common root and want to return it. The top solution (Tal) has a similar problem. If x or y equals the current value, the function falls off the end without returning anything. I was trying to improve upon it and ended up making a bigger mistake. Sorry, folks.

Aug 1, 2013
 Consider an X x Y array of 1's and 0s. The X axis represents "influences" meaning that X influences Y. So, for example, if \$array[3,7] is 1 that means that 3 influences 7. An "influencer" is someone who influences every other person, but is not influenced by any other member. Given such an array, write a function to determine whether or not an "influencer" exists in the array.12 AnswersThis was a tough one that forces you to consider how best to traverse the array and eliminate possibilities as soon as possible.Not_Influencers[n] = 0; //Make all elements 0 for (i = 0 ; i< n ; i++){ if(Not_Influencers[i] == 1) continue; row_sum = find_row_sum(a[i]); if(row_sum == n-1 && find_col_sum(i) == 0) return Found; for(j = i; j < i; j++) if (a[j] == 1) Not_Influencers[j] = 1; }X should be equal to Y, right?Show More Responses//if vec[i][j] == 0 then i is not an influence //if vec[i][j] == 1 then j is not an influence //so time complexity is O(n) bool find_influences(vector > &vec) { int n = vec.size(); vector not_influence(n); for (int i = 0; i = 0; --j) { if (!vec[i][j]) { break; } not_influence[j] = 1; } if (j < 0) { return true; } } not_influence[i] = 1; } return false; }Run a BFS or DFS. For each node keep going to influencer. Find a node which can be reach by all nodes. Sort of finding sink node.public static int influencer(final int[][] jobs, final int r, final int c) { int[] degree_in = new int[jobs.length]; int[] degree_out = new int[jobs.length]; for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { if(jobs[i][j] == 1) { // i influences j degree_out[i]++; degree_in[j]++; } } } for (int i = 0; i < r; ++i) { if (degree_out[i] == r - 1 && degree_in[i] == 0) { return i; } } return -1; }Consider the input as Graph given in adjaceny matrix representation. Find whether a semi-eulerian path is present in the graph or not.Take the XOR product of the original matrix with the transposed matrix and sum by row. If any row counts equal the rank then they are influencers.private static boolean hasInfluencer(int[][] matrix) { if (matrix == null) return false; if (matrix.length == 0) return false; boolean result = false; for (int i=0; ithe XOR suggestion I think is incomplete. The condition sum(row_influencer) = 1 and sum(column_influencer) = N so a simple matrix multiplication with the transposed should give for the vector v[influencer] = N and v[N-influencer] = 1. I assume influencer influences himself.def find_influencer(matrix): for row in range(len(matrix)): following_none = not any(matrix[row]) if not following_none: continue all_following = True for r_no in range(len(matrix)): if not row == r_no: continue if not matrix[r_no][row]: all_following = False break if all_following: return row return -1Here is a different view. Please comment if you find any issues with the logic. 1st. condition: An influencer can not be influenced by any one. Let's say the in a matrix of [x.y], there is an influencer with index 2. So, the column=2 (3rd column) in the matrix must be all 0s, since the influencer can not be influenced. Step 1: Find a column with all 0s. If found, remember the column index or there is no influencer. Let's say, it is m Second condition: An influencer must have influenced everyone. So, in our example: row=2 (third row) must be all 1s except for column=2, since influencer can not even influence self. Step 2: Check row=m and find that all values are 1 except for [m][m]. If found, we have an influencer.

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

Sep 9, 2009
 Convert a string to an integer6 AnswersNot exactly super, but should do for a whiteboard: int myatoi (char *str) { int retval; int sign; sign = 1; if (*str == '-') { sign = -1; str++; } retval = 0; while (*str) { retval *= 10; retval += *str - '0'; str++; } return (retval * sign); }I think, there is bug at statement if (*str == '-1') ........ what if str = "-1234"Bug, The check is just for the first character of the string - your version will not even compile...Show More ResponsesThe pointer is a char pointer, so the loop (and the sign check) are on a character by character basis.Brian is right, he is using C char pointers, and can compare a character at a time. This is not Java, this does work.int.Parse(str) hah. But in all seriousness, the aforementioned solution is good.

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

Jan 28, 2011
 Explain the data structure you would use to implement pop() and push(Object, int) for a Priority Queue.6 AnswersI had said red black tree. He seemed to agreeWould a heap not be more appropriate?Did you get the job?Show More ResponsesNo ... I did mention it in my review. Interviewed and No offer.I got this question as well. However, the answer is impossible in constant time. A self balancing tree is one way to implement this. In that scenario you have O(log n) time for insertion and removal. You can always have the get minimun in constant time. A van Emde Boas tree or Fusion Tree can speed up this time, but not bring it to constant time.Heap is the one to choose. Priority queue always pops out the highest valued element(the highest value is defined using weak ordering criterion), pop_heap will make sure the second highest element is placed on top after the first one is popped out, push_heap will also make sure the highest value is placed on top of the queue. In many cases, this is all you want - the highest valued element, which is what priority queue is operating on..