Senior Engineer Interview Questions | Glassdoor

# Senior Engineer Interview Questions

10,877

Senior engineer interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

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

Apr 21, 2011
 In a stream of integers from 1 to n, only one number will be repeated. How can you tell what that number is?10 AnswersThis felt like a brain teaser question to me, and since I hadn't heard it before it took me a little while to come up with a solution that involved using a factorial function.You know n. S = n*(n+1)/2 is the sum of 1st n numbers. P = sum of the n+1 numbers you are provided with. Finding P given an array of n+1 integers can be done in O(n). P - S is the repeated integer.Heres an explanation, http://www.techinterview.org/post/526329049/sum-it-upShow More Responsesint sum = 0; int xorSum = 0; for(int i =0 ; i < n; i++) { sum += input[i]; xorSum ^= input[i] } return (sum - xorSum)/2;Mat, try 1,2,2,3: 1+2+2+3= 8 1^2^2^3= 2 (8-2)/2=3?2A hash table would resolve the question with O(n)Add to HashSet. It will return true if it exists.If you're writing it in ruby def find_repeat(numbers) numbers.length.times{|n| return numbers[n] if numbers[n] != n } endlmao, ok everyone getting a little craycray, why not just simply this.... int prev << stream; while (stream) { int curr << stream; if curr == prev return else prev = curr; }(sum_of_numbers - (n*(n-1)/2))

Jul 29, 2010
 what's wrong with the following code :

Jul 13, 2009
 Create a stack of numbers where the maximum number is always known.7 AnswersCreate a separate stack for the maximums.maintain a sorted stack, the insert would look like insert(int p,stack s){ stack large; int top = s.peek(); while(top>s){ large.push(s.pop()); top = s.peek(); } s.push(p); while(!large.empty()){ s.push(large.pop()); } }sorry, typo while(top>p)Show More ResponsesI was asked of this question as well. I not sure how to implment a second stack in a way that the max number is always on top.I was asked of this question as well. I not sure how to implment a second stack in a way that the max number is always on top.To Job Seeker: The basic idea is that when a new number is being pushed onto the stack, you need to see if that number is greater than or equal to the current top of the maximums-stack. If it is, you push the number onto maximums-stack as well. Also, when pop is called, you look to see if the number being popped is equal to the number on the top of the maximums-stack. If it it, pop that stack as well.just saw this, i think u can just map your number into an object, that has both the maximum number, and the last number that you've pushed in just peek the stack before you push, compare the peek'd value 'vs' your new pushed number, then replace or update the max number as you see fit? if you're only allowed to push numbers into your stack, just push your number in the stack more than once (at-least three times) indicating that every value that you put in is a sequence of 2 numbers, and so, push another one (the 3rd, which will always be your maximum number) so on every 3 pushes, you've stored the maximum value, then everytime you push something in, just peek the last 3 values in the stack, knowing that the 2nd and 3rd value was probably the number you pushed in, and the first 1 was the max value it's funny

Apr 7, 2010

Apr 27, 2013
 Given a series of words written using a scrambled alphabet, figure out what order the letters of the alphabet are in.7 AnswersUse a graph!More than three words would be helpful. Do what with a graph? How would you build it? How would you search it?Given a ciphertext C, and plaintext P, the problem is to find a key K that converts the ciphertext to plaintext. Without considering the asymptotic complexity, it should be straighforward to generate the key combinations using a backtracking search algorithm.Show More ResponsesMore specifically, you would be given a list of words in the ciphertext which are in alphabetical order. Your job is to determine the alphabetic order of the ciphertext letters. In a graph, each node would represent a letter, the directed edge would indicate ordering of the two letters it connects. Once the graph is built you can do a simple traversal of the graph to generate your alphabetical ordering. Hope that helps!I think I didn't get the question. Could someone elaborate on it a bit please?@Alexander An example of what I believe the question is presenting to the applicant is as follows. Say I knew one of the words was 'CAT'. Now, if I look at the scrambled 3 letter word, and it was ZBS, then I know that C = Z, A = B, and T = S. Essentially, potentially every letter in the alphabet (up to 26 letters in total) are not of the same value. So, if I had another example word, say 'TACS', and the given scrambled word is 'SBCY', then I now know that S = Y as well. This is not an explanation of how to approach the problem, as we're not given any sample words; This is simply my interpretation of how the question is supposed to be interpreted as @Alexander is probably not the only person who does not understand the question. Of course, I could have it wrong too, so who knows.http://www.careercup.com/question?id=19114716

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

Sep 28, 2011
 Given an array of integers A[1...n], compute the array B[1...n] such that B[k] is the product of all the elements of A, except A[k]. Part ii) Try to do it without division (some mobile devices don't have division). Was asked to write code for part ii.7 AnswersHere is a psedocode for the same. public void solveAns() { Integer a[] = {1,2,3,4,5}; Integer b[5] = new Integer[5](); for ( i =0; i<5; i++) { b[i] = getProduct(a, i); } } private Integer getProduct(Integer[] a, int index) { Integer prod = 1; for ( i =0; i@Shilpa while your answer is correct, it is O(n^2). This can be done in O(n) with no auxillary space. Two passes of the array A : After first pass B[i+1] = A1 * A2 * A3 * .... Ai So in second pass, by traversing A in reverse order and multiplying we can get the desired result.@AV seems your method is still O(n^2) or a extra array is needed; like this: void product (int * a, int* b,int n) { int * c= new int [n]; for (int i=0;iShow More Responseslong Totl = 1; for ( i = 0; i < length; i++ ) // Get total product { Totl *= arry1[i]; } for ( i = 0; i < j; i++ ) { if ( arry1[i] ) prdArry[i] = Totl / arry1[i]; else prdArry[i] = 0; }It can be almost done in O(n) time in two steps. #1. Determine the product of all numbers O(n) #2. Divide the product at its index & store it to b. Complexity O(n); For example, Let a[] = {1,2,3,4,5};//make sure doesn't have zero. and b[] as a new array; int product=1; for(int i=0; iLinear time guys. #include "stdafx.h" #include using namespace std; void printmultipliers(int a[], int b[], int n) { int totalProduct = 1; cout = 0) { if(j == (n -1)) { cout << b[j -1] << " "; } else if(j == 0) { cout << reverseproduct; } else { cout << b[j-1] * reverseproduct<< " "; } reverseproduct *= a[j]; j--; } } int _tmain(int argc, _TCHAR* argv[]) { int a[] = {1, 2, 3, 4}; int b[4]; printmultipliers(a, b, 4); return 0; }Another take on O(n) (or may be what AV was saying above). It takes a couple extra arrays, but better than O(n^2) int[] a = new int[] {2, 3, 4, 5, 6}; // should render final = [360, 240, 180, 144, 120] int len = a.length; int[] before = new int[len]; // will become [1, 2, 6, 24, 120] int[] after = new int[len]; // will become [360, 120, 30, 6, 1] int[] final = new int[len]; // will be [360, 240, 180, 144, 120] int front = 1; int back = 1; before[0] = 1; after[len - 1] = 1; for (int i = 1; i < len; i++) { front = front * a[i - 1]; // first pass 2, second 6, etc. before[i] = front; // end up with [1, 2, 6, 24, 120] after = after * a[len - i]; // first pass 6, second 30, etc backward[len - i - 1] = after; // filled backwards so we don't have to mess with this below. } for (i=0; i < len; i++) { final[i] = before[i] * after[i]; } return final;

Apr 24, 2010
 Intersection of two numerical arrays6 AnswersAlgorithm and pseudo code* assuming b[] is the longer array quick sort b[] for all items from a[] binary search this item in b[]for above.. O(n log n) + O(n) * O(log n)Show More ResponsesI have a O(n) algorithm: 1. Iterate over all the elements of first array a[] and build a dictionary mapping the element value to the index - O(n) 2. Now iterate over all the elements of the second array b[] and for each element that is already present in the dictionary, move the element to a different array that maintains the intersection elements - O(n) 3. Hence the overall complexity is O(n) in time and O(n) for the dictionary and the array to maintain the intersection elements.given arrays of lengths n and m. A simple solution is to sort array n in O(n lg n) and for each item in array m, look for it in sorted n, O(m lg n). So total time = O((m+n)lg n), let array n be the short array. I feel there is a much quicker solution, maybe O(n+m) if we assume integers.Two possible approaches: 1. Sort both arrays and then walk over each array simultaneously until you find all the common entries. This is O(n*logn) to do the sort and then walking over the items is O(n). 2. Walk over first array and insert each item into a hash table. Then search for each item in the hash table. This is O(n) time and O(n) space. If you're doing this a lot with the same sets of data, both algorithms allow you to do the expensive step once for each array and then find the common items in linear time.

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

Mar 18, 2009
 Write a program to count the number of words in a file.5 AnswersYou have to code a program over the phone.What I would look for in an answer here would be for the person to focus on the definition of what delimits a word. Then a state machine construct that counts the number of transitions from the "in a word" state to the "out of word" state would be the word count * 2.#!/usr/bin/perl -w my \$cnt = 0; while() { my \$num_blanks = s/ +/ /g; \$cnt += \$num_blanks + 1; } print "\$cnt\n";Show More Responseswc -l Not a program per se, but works :)#!/usr/bin/bash echo "Enter filename" read fileName for WORD in \$(cat \$fileName) do echo \$WORD done | wc -l

Mar 18, 2009