Programming Interview Questions | Glassdoor

# Programming Interview Questions

212

interview questions shared by candidates

## Programming Interview Questions

Sort: RelevancePopular Date

### Director of Marketing and Communications at Loyaltyworks was asked...

Sep 10, 2014
 What is your current employer doing better than we are? I work in marketing so I had to answer based on my research done prior to interview. I was so happy I did a ton of research/comparison before going in.1 AnswerThey do a better job of promoting and communicating their programs.

Jun 8, 2009

Jun 11, 2010

Feb 27, 2010

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

Jun 24, 2010
 List all anagrams in a file. Assumptions: case-insensitive, a-z characters only, one word per line. For example, if the file contains dog, cat, ddd, goo, act, god -- output dog, god, act, cat10 AnswersThankfully I was taking a theory course and one trick used in the course was "encoding" programs as a large natural number using product of primes. 1. Adapt the encoding as follows -- generate the first 26 primes and assign a prime to each letter 2a. For each word, take the product of each letter's prime. So #(act) = 2*5*prime(t) 2b. As you can see, #(cat) = 5*2*prime(t) = #(act) 3. Insert a handwavey argument about inserting the number/word pairing into a HashMap>Sort the words. Anagrams appear next to each other in the sorted list.Sorry, sort the letters in each word, then sort the words. Anagrams appear next to each other in the list. For example the sorted list for the input would be: act act ddd dgo dgo gooShow More ResponsesThanks for sharing Bill For this set of input, the expected output should contain only [cat, act, god, dog]. I'm curious to see what "next steps" your algorithm will perform to provide this expected outputYou keep track of the mapping from the sorted word to the actual word in a pair, for example: [act, cat] [act, act] [ddd, ddd] [dgo, god] [dgo, dog] [goo, goo] Then you go through this list and count if you have a duplicate entry or not. If you do, like for act, you print out those duplicate entries: cat, act.Bill, your algorithm is O(n*log(n)) while the candidates would be O(n) - provided he uses a decent hash functiondonutello, bills algo is not n log n it is n*log(k) where as candidates algo is n * k again (multiplications for each word) where k = length of the longest word on top of that calculating primes is expensive anyway I would go with bills answerBills algo is nlogk + nlgn. After sorting the k letters for n times you also have to sort the n words.#Get inputs a = [] f = open('input.txt','r') for line in f: line = line.strip() a.append(line) #Sort letters in a word def sort_letter(s): r = [] for i in s: r.append(i) t = sorted(r) v = ''.join(t) return v #Find anagrams d = {} for v in a: sv = sort_letter(v) if sv in d: d[sv].append(v) else: d[sv] = [v] #Print results for k,v in d.items(): if len(v) > 1: for s in v: print sthink of each line as a set of characters, not a word, then create set of sets of characters which you fill from the input. then print the set (order does not matter as its not specified)

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

Mar 19, 2009

Oct 1, 2010
 Given a matrix print it clockwise from the first element to the very inner element.6 AnswersNeed to design the program first and then implement it very carefully yet fast!void PrintMatrix(int** a, int n) { for (int i = 0; i = i; j--) { printf("%d ", a[n - i -1][j]); } } void PrintColInc(int** a, int n, int i) { for (int j = i; j = i; j--) { printf("%d ", a[j][i]); } }struct point { point(int a, int b) { row = a; col = b; } int row; int col; }; void printMatrixSpiral(int a[], int r, int c) { int direction = 1; // 1 - right // 2 - down // 3 - left // 4 - up point upper_left = point(0,0); //point upper_right = point(0,c-1); point lower_right = point(r-1, c-1); //point lower_bottom = point(r-1, 0); while(true){ if(direction==1){ int row = upper_left.row; for(int i=upper_left.col; i= upper_left.col;--i){ cout= upper_left.row;--i){ cout lower_right.row) || (upper_left.col > lower_right.col)){break;} } }Show More Responsesint matrix[] = { {1, 2, 3}, { 4, 5, 6}, {7, 8, 9} }; void PrintMatrix() { int start = 0; int end = 2; while(end >= start) { for(int col = start; col = start; col--) cout start; row--) cout << matrix[row][start] << " "; start++; end--; } }int main() { int m, n; cin >> m >> n; int matrix[m][n]; for (int i = 0; i > matrix[i][j]; int l = 0, r = n, t = 0, b = m; while (l = l; i--) cout = t; i--) cout << matrix[i][l] << ' '; l++; } return 0; }^ Above anon, your solution is almost correct. But for the 3rd and 4th for loops you first need to check whether you've already printed the corresponding previous for loop. Ok I didn't explain that too clearly. Here's an example: Matrix: {{1,2,3,4},{5,6,7,8},{9,10,11,12}} will print 1 2 3 4 8 12 11 10 9 5 6 7 6 That last 6 is because of the 3rd for-loop. You don't need to run that opposite for-loop (R-L) once you've already printed 6 from L-R. Just use an if-loop to rectify this. Same for the 4th loop.

May 9, 2012