General Merchandise & Superstores interview questions | Glassdoor

# Interview Questions

14,131

interview questions shared by candidates

## Interview Questions

Sort: Relevance Popular Date

Oct 15, 2009

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

Jun 8, 2009
 Number of 1's in binary representation of integer? 12 Answers Run a loop, in which you binary-AND the integer with 1, and increment a counter, if the result is 1. Then right-shift the input-integer by 1 bit, and start over in the loop unsigned int ones(unsigned int number) { unsigned int n; for (n = 0; number != 0; number >> 1) { if (number & 1) { n++; } } return n; } unsigned int ones(unsigned int number) { unsigned int n; for (n = 0; number != 0; number >> 1) { if (number & 1) { n++; } } return n; } Show More Responses i dnt knw wheather it correct or not.....do correct me if im wrng a=0 q=n/2 r=n%2 n=q if(r=1)then a=a++ continue.... ct = 0; while (val) { ct++; val = val & (val - 1); } Several of the above work, but use preincrement public static int population(int x) { x = (x & 0x55555555) + ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F); x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF); x = (x & 0x0000FFFF) + ((x >>16) & 0x0000FFFF); return x; } in C++, how about: do { sum += n&1; } while (n>>=1); int ones( ) { int n; int number = 1100110111; n = 0; while (number!=0) { int temp = number % 10; if(temp ==1 ) n++; number = number/10; } return n; } Lets consider 14(1110) is the number int CountOnes(int Number) { int n=0; while(number !=0) { if(number%2==1) n++; number >> 1; } return n; } The function takes an int and returns the number of Ones in binary representation public static int findOnes(int number) { if(number < 2) { if(number == 1) { count ++; } else { return 0; } } value = number % 2; if(number != 1 && value == 1) count ++; number /= 2; findOnes(number); return count; } All the answers above will not get you to amazon... try code the function with o(m), where m is the number of 1's in the binary representation of an integer. (hint: look up "Programming Interviews Exposed")

Dec 6, 2012

Apr 28, 2009

Mar 1, 2012

Feb 27, 2010

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

Mar 29, 2011
 Write an algorithm to determine whether a given number is of the form (2^n)+1, where n is an integer. 17 Answers Subtract one and then keep dividing by two until you either get to 1 (true) or the number is not evenly divisible (false). return (n == 1) || ((n-1)&(n-2) == 0); bool if_power(int n) { n--; return n&(n-1) ? false : true ; } Show More Responses if(n%2==1) return TRUE else return FALSE boolean isapower(int pow, int num); int main(){ int num = 2; int pow = 2; if(isapower(pow, num-1)) { printf("%d is power of %d + 1", num, pow); } else { printf("%d is not power of %d + 1", num, pow); } return 0; } boolean isapower(int pow, int num) { while((num > pow) && (num % pow == 0)) { num /= pow; }; return num == pow; } if(n & (n-1))==(n-1) return true else return false The simplest answer would be: (number & 1) == 1. The last bit should be 1. Sorry. That answer is for checking odd or even. Not power of 2 + 1. The interview candidate's answer does not account for (2^0) + 1. It would return false for that. Another option. Assuming Int x contains the value: boolean correctForm(int x) { x--; String binString = Integer.toBinaryString(x); if (binString.charAt(0) == '0') { return false; } for (int i = 1; i < binString.length(); i++) { if (binString.charAt(i) != '0') { return false; } } return true; } (number - 2) in binary must be all 1s because 2^n-1 is its binary form must match regexp \$0*10*1^ public class isPowerOf2 { public boolean isPower(int number){ if(number <=0){ return false; } number = number -1; if((number & -number) == number) return true; else return false; } boolean isPower(int num){ num--; Boolean foundOne = false; for(int i = 1; i < sizeof(int); i++){ if((1<

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

Mar 19, 2009
 Given a string find the first non-repeated character. 10 Answers Hint: use a hash table public static char getFirstNonRepeatedChar(String s) { List charList = null; char nonRepeatedChar ='?'; if (s != null) { s = s.trim(); charList = new ArrayList(); for (int i=0; i 1): continue else: return x return "No nonrepeating character found" for (int index = 0; index < str.Length; index++) { if (str.LastIndexOf(str[index]) == str.IndexOf(str[index])) { return str[index].ToString(); } } I will have one variable x to store the first non repeating character, variable y as a backup And a set of characters already encountered. I will start from position n to position 0 where n is the lenght of the string. If char(n) is not present in the set, then check if x=char(n), if yes, x = y. If no, y=x and x=char(n). The idea is to keep updating the newest character that has not been repeated and also keeping the second newest character as a backup. In the end return x.

Mar 19, 2009