Programming Interview Questions | Glassdoor

# Programming Interview Questions

214

interview questions shared by candidates

## Programming Interview Questions

Sort: RelevancePopular Date

### 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

Apr 27, 2012
 Write a program that given 4 coin denominations and a dollar amount finds the best way to express that amount using the coins given. I.e. you have coins with denominations of 1c, 7c, 13c,19c and you have to express \$2.12 with the least number of coins. There is always a 1c coin but the other 3 are arbitrary.6 AnswersTurns out , this problem only has an elegant solution when the coin denominations are divisible by each other, such as the US coins (1, 5, 10, 25). Otherwise, it requires a (slightly optimized) brute force algorithm. I was told so by the interviewer after struggling with it using different approaches for about 40 minutes.If you have a coin of value 1, you can use a greedy algorithm: always select the most valued coin, until you have less money left that this value. Remove the coin, try again with less coins and the money left. Otherwise, just bruteforce and memoization, to implement a simple dynamic programming approach where you want to minimize the number of coins used, caching on the amount of money left to divide.Greedy algorithm is not right at all for general cases. This is a very classical questioin. If you fail on this question, I would say that it is probably your fault.Show More ResponsesIn all my years of giving and receiving interview questions I have never seen this one. It is definitely NOT a classical programming problem. Asking whether a candidate knows of an obscure academic puzzle does not sound like a good interview question.int main() { int currency[] = {19,13,7,1}; int money = 212; int i=0; int div=0; while (money>0) { div = money/currency[i]; money = money%currency[i]; if(div>0) { coutIt is a classical programming question that derives from the first fit decreasing algorithm in discrete (decision) mathematics.

### Software Engineer at Goldman Sachs was asked...

Jul 31, 2010
 How to remove a node from a singly-linked list when only given the pointer to the node8 AnswersYou have to use some pointer/memory trickerythisNode.data = thisNode.next.data temporaryNode = thisNode.next.next delete thisNode.next thisNode.next = temporaryNodeThe post by "jobseeker" assumes that we know the node BEFORE the node to be deleted. This assumption is not consistent with the problem definition, but makes it solvable.Show More ResponsesMilly, jobseeker's assumption is reasonable, we should not delete the node before removing it from the list (if it was created by the list)Milly: jobseeker is right in that you don't delete the "node to be deleted". You delete the node that FOLLOWS the "node to be deleted", only after swapping the values of the two.node ->val=node->next->val; node ->next=node->next->next;node = node.next;node.val = node.next.val; node.next = node.next.next;

May 9, 2011

Nov 8, 2010

Feb 10, 2013

### Software Engineer at PayPal was asked...

Nov 10, 2011
 Consider the following function: int f (int num) { int out = 0; for (; num > 0; num /= 10) { int d = num % 10; out *= 10; out += d; } return out; } 1) What does it do? 2) Write the same algorithm using recursion5 AnswersThere is a trick that need global variable or static variable to record the temper sum. so the function declaration cannot be same as loop version if don't use global or static variable. the output parameter b will be the reverse result. void rev(int d, int& b) { b *= 10; if (d/10 == 0) b += d; else { b += d%10; rev(d/10, b); } }You can do it without the golbal or static variables: int f(int num) { if((num/10) == 0) return num; else return ( 10(num%10) + f(num/10) ); }Sorry my answer previously posted was wrong. Here is the correct one without golbal or static variables, which is basically the same as the first one by Paul. inf f(int num, int car=0) { car *=10; if((num/10) == 0) return ( car + num ); else return f(num/10, car+(num%10) ); }Show More Responsesprivate static int reverse(int n, int r) { if (n == 0) return r; return reverse(n/10, 10*r+n%10); } public static int reverse(int n) { return reverse(n, 0); }Simple method without using global variable. static int reverseRecursive(int number, int sum) { if (number != 0) { int temp = number % 10; sum = sum * 10 + temp; return reverseRecursive((number / 10), sum); } else return sum; }

### Game Programmer at Zynga was asked...

Feb 8, 2012
 How do you find the max depth of a binary tree?5 AnswersI used recursion, passing the current depth to each child and returning the max value they returned plus one to the caller.how about bfs?breadth first search will be better than the recursion method for min-depth searching, but works same as recursive in the case of finding max depth problemShow More Responsespublic static int maxDepth(TreeNode root) { if (root == null) return 0; //base case return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; }This entirely depends on the implementation. It can be calculated as the integer log base 2 of the highest index of the tree. This would prevent quite a few cache misses from traversing the tree each time, and would be a fairly quick calculation using bitwise operations. int val;// the value int result;// the result int tmp; result = (val > 0xFFFF ? 0 : 1) >= result; tmp= (val > 0xFF ? 0 : 1) >= tmp; result |= tmp; tmp= (val > 0xF ? 0 : 1) >= tmp; result |= tmp; tmp= (val > 0x3 ? 0 : 1) >= tmp; result |= tmp; result |= (val >> 1); While this may seem like a lot, assuming the tree is small, these calculations are actually quite fast, don't involve branching, and don't require arbitrary pointers to sub-tree members. This works best for trees that are either complete or nearly complete, however, since otherwise there will be a lot of wasted memory for indices that contain nothing.

### Software Developer at Epic was asked...

Aug 29, 2009
 Please write a function that accepts a floating number and returns its square-root. You may not use built-in square root function from your language. However, basic operators like addition, subtraction, multiplication are allowed. Please take into consideration the floating precision.3 AnswersHint: please really take into consideration the floating point.Newton's method: http://en.wikipedia.org/wiki/Newton's_methodhttp://www.dreamincode.net/code/snippet244.htm

### Software Developer at Epic was asked...

Jul 31, 2009
 Write a function which, given n, prints all well-ordered integers of n digits. A well ordered number is one where the value of the i-th digit is less than the value of the i+1 digit.5 AnswersWhat does it mean by "all well-ordered integers"? For example, if n=3, then should "all well-ordered integers" look like this: 1, 2, 3, 12, 13, 23, 123?@anonymous: it means print all integers of 'n' digits, where the digits are in ascending order: for n=3: 123, 234, 345 ...789Are you sure? There is no upper bound then. What is your upper bound? How do you determine the upper bound? I believe you misunderstood the question.Show More Responses123, 124, 125 ... 129, 134, 135 ...139 ...234 ... 289 ...789The upper bound is the n digit not with all 9s
1120 of 214 Interview Questions