Apple Interview Question

Find number of ones in an integer.

Interview Answers

Anonymous

Nov 2, 2011

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; }

13

Anonymous

Oct 22, 2011

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++; }

10

Anonymous

Apr 14, 2012

int 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; }

2

Anonymous

Feb 12, 2012

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 ones

1

Anonymous

Sep 27, 2013

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

1

Anonymous

Nov 11, 2013

Convert to binary and iterate. Check last big (increment counter if 1) then bit shift right to drop last bit. repeat.

Anonymous

Apr 12, 2014

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); }

Anonymous

Oct 24, 2015

Perl read fm stidn: chomp ( my $int = ); my $ones = $int =~ tr/1/1/; say $ones;

Anonymous

Feb 14, 2012

just an idea #!/usr/bin/env ruby 123456718791.to_s.scan(/1/).size

1

Anonymous

Oct 24, 2011

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.

Anonymous

Oct 1, 2012

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; }

Anonymous

Oct 19, 2011

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

1

Anonymous

Oct 24, 2011

This 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;