Software Engineering Interview Questions in San Jose, CA | Glassdoor

Software Engineering Interview Questions in San Jose, CA

6,130

Software engineering interview questions shared by candidates

Top Interview Questions

Sort: RelevancePopular Date

Aug 8, 2011

Apr 1, 2011

Nov 26, 2011

Nov 7, 2010

Nov 20, 2009

Jul 13, 2009

Oct 19, 2010

Software Engineer at Apple was asked...

Oct 15, 2011
 Find number of ones in an integer.13 Answers1. Convert the number into bits (we assume that all our bits of size 8) 2. "And" it with 1 3. check if the bit returned is 1 or now. if it is '1' increase some counter. 4. right shift 8 times and perform go to 2or you can do the following using properties of number. every number when AND'ed with number - 1 it would eliminate one binary number.\ count = 0; while(n) { n &= (n-1); count++; }This doesn't work: 1. Convert the number into bits (we assume that all our bits of size 8) 2. "And" it with 1 3. check if the bit returned is 1 or now. if it is '1' increase some counter. 4. right shift 8 times and perform go to 2 look at the number 11: counter=0; 0x000B, look at the last byte B: 1011 and that with 1, you get 1 counter now equals 1, right shift 8 times and get 0x0000 and with 1, get 0, doesn't increment counter. so the number of 1's in 11 is 1.Show More ResponsesThis doesn't work either: or you can do the following using properties of number. every number when AND'ed with number - 1 it would eliminate one binary number.\ count = 0; while(n) { n &= (n-1); count++; } Look at n=7, which we know has no 1's in it. 7 in binary: 0111 6 in binary: 0110 n=7&6; so n=6; counter=1; loop again: 5 in binary=0101; n=6&5=0110&0101=0100=4; counter=2; We can already see this is wrong answer should be 0;Solutions provided above are used for finding out the number of ones in a binary string. If you Google for such question, both proposed answers would show up. For finding out the number of ones in an integer, I would propose the following. int NumberOfOnes(double number) { int counter = 0; if (number == 0) return 0; if (number == 1) return 1; do { if (number % 10 == 1) counter++; number = number / 10; } while (number); return counter; }Binary ones in python: def findBinOnes(integer): val = integer ones = 0 while val > 0: if val & 1 == 1: ones +=1 val = val >> 1 return ones Decimal ones in python: def findDecOnes(integer): val = integer ones = 0 while val > 0: if val % 10 == 1: ones += 1 val = val / 10 return onesjust an idea #!/usr/bin/env ruby 123456718791.to_s.scan(/1/).sizeint numOfOnes( int n ){ int num = 0; String str = Integer.toString ( n ); for(int i = 0 ; i < str.length() ; i++ ) { if ( str.charAt ( i ) == '1' ) num++; } return num; }In Perl: sub OnesInNum{ my \$number = shift; my \$count; my @numbers = split //, \$number; foreach my \$num (@numbers){ next if(\$num eq '.'); #skip decimal point \$count++ if(\$num == 1); } return \$count; }#include #include using namespace std; int CountBits(const int& number){ int test = 1; int count = 0; for (int i = 0; i < (sizeof(number)*8) ; i++){ int filter = number & test; if(filter) count++; test <<= 1; } return count; } int _tmain(int argc, _TCHAR* argv[]){ cout << CountBits(566632145) << endl; return 0; }Convert to binary and iterate. Check last big (increment counter if 1) then bit shift right to drop last bit. repeat.int main(void) { int input, counter = 0; printf("Enter integer: "); scanf("%d", &input); while(input) { if ((input & 0x0001) == 1) {counter++;} input = input >> 1; } printf("No. of 1s are: %d", counter); }Perl read fm stidn: chomp ( my \$int = ); my \$ones = \$int =~ tr/1/1/; say \$ones;