Apple Interview Questions | Glassdoor

# Companies matching "Apple"

Apple Interviews

www.apple.com /  HQ: Cupertino, CA

5,243 Interviews

3.0 Average

A is for Apple Interviews

www.aisforappleinc.com /  HQ: San Jose

28 Interviews

1.7 Very Easy

Apple Vacations Interviews

www.applevacations.com /  HQ: Newtown Square

17 Interviews

2.4 Easy

## Apple Interview Questions

Sort: RelevancePopular Date

Oct 6, 2010

Jun 19, 2012

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

Jul 21, 2013

### Senior Software Engineer at Apple was asked...

Apr 21, 2011
 In a stream of integers from 1 to n, only one number will be repeated. How can you tell what that number is?11 AnswersThis felt like a brain teaser question to me, and since I hadn't heard it before it took me a little while to come up with a solution that involved using a factorial function.You know n. S = n*(n+1)/2 is the sum of 1st n numbers. P = sum of the n+1 numbers you are provided with. Finding P given an array of n+1 integers can be done in O(n). P - S is the repeated integer.Heres an explanation, http://www.techinterview.org/post/526329049/sum-it-upShow More Responsesint sum = 0; int xorSum = 0; for(int i =0 ; i < n; i++) { sum += input[i]; xorSum ^= input[i] } return (sum - xorSum)/2;Mat, try 1,2,2,3: 1+2+2+3= 8 1^2^2^3= 2 (8-2)/2=3?2A hash table would resolve the question with O(n)Add to HashSet. It will return true if it exists.If you're writing it in ruby def find_repeat(numbers) numbers.length.times{|n| return numbers[n] if numbers[n] != n } endlmao, ok everyone getting a little craycray, why not just simply this.... int prev << stream; while (stream) { int curr << stream; if curr == prev return else prev = curr; }(sum_of_numbers - (n*(n-1)/2))Add each integer to the map if it doesn’t already exist as key if it’s exists then it is a repeated number.

Mar 8, 2010

Sep 19, 2011

Nov 5, 2012