Apple Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
8
of 8 vote

Sum of Odd numbers
1*1 = 1 // 1
2*2 = 4 // 1+3
3*3 = 9 // 1+3+5
4*4 =16// 1+3+5+7

and so on

- Abhay August 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you know when stop counting your negative numbers? you only have the number (2, 3, 4) ...

- Serjay April 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int sq(int num){
	
	int i,sum=0,j=1;
	for(i=0;i<num;i++,j+=2)
		sum+=j;
	return sum;
}

- newGuy April 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

int squareNum(int num){
	int answer = num;
	for(int i = 1; i<num; i++){
		answer = answer + num;
	}
	return answer;
	
}

- Dxn7335 August 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is wronggg....

- Anonymous September 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why this is wrong? It seems right...

- Anonymous April 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why this is wrong??
It seems right...

- PJ April 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

int Sq(int n){
		int i=n, sq=0, count=1;
		if((i&1) == 1)
			sq += n;
		i >>= 1;
		while(i>0){
			if((i&1) == 1)
				sq += n<<count;
			i >>= 1;
			count++;
		}
		return sq;
	}

- Sathya August 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But seems to be O(log n), so maybe this is another gem.

- Anonymous August 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What are "i&;1" and "i>;0"? Can careercup fix the display problems?

- liu425 December 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

def mult_bitop(n):
  s=0
  c=n
  while (n>0):
    #print n,c,s
    if (n&1):
      s+=c
    c<<=1
    n>>=1
  return s

- gen-y-s August 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I see that it works but can someone pls give an explanation ?

- iwanna November 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

int Sq(int n)
{
if (n<2) return n;

int i = n>>1;
if (n&1) return ((Sq(i)<<2) + (i<<2) + 1);
else return (Sq(i)<<2);
}

- Chang November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

3 lines are enough

- Chang November 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bad question for an interview, but a very interesting puzzle.

Anyway, here is a recursive method (in python), which is O(log n) arithmetic operations (which does not use * or exponentiation).

def square(n):
   if n == 1:
       return 1
   k = n/2
   x = square(k)
   if n % 2 == 0:
        # n = 2k, x = k^2
        return x << 2
   # n = 2k+1, x = k^2
   # n^2 = 4k^2 + 4k + 1
   return x << 2 + k << 2 + 1

And yeah, the interviewer needs a stick (get it? carrot, stick...).

- Subbu August 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Some right bracketing needed to take care of operator precedence issues with + and <<.

- Anonymous August 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ugh. 0 votes for this gem.

- Anonymous August 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

square(n) = n times n(n+n+n+.......+nx where x = n)

- shukad333 August 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ugh.

- Anonymous August 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int square(int a) {
	return multiply(a, a);
}

public int multiply(int a, int b) {
	if (b == 1) {
		return a;
	}

	int temp = (b % 2) == 0 ? 0 : a;
	int ret = multiply(a, b / 2);
	return temp + ret + ret;
}

- Anonymous September 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good that you tried to generalize. But, you should pick the smaller number to halve (i.e. check if a < b, and if so, try (a/2, b) etc).

For example: multiply(1, large_number)

- Anonymous September 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int square(int a) {
	return multiply(a, a);
}

public int multiply(int a, int b) {
	if (b == 1) {
		return a;
	}

	int res= multiply(a, b / 2) << 1;
	return b % 2 == 0 ? res : res + a;
}

- Anonymous September 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try with logarithm ?
ln(a²) = 2*ln(a) = ln(a) + ln(a)

public long square(long n) {
	return Math.round(Math.exp(Math.log(n) + Math.log(n)));
}

- Pignolo September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

num/(1/num)

- Anonymous September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not define for 0

- broken September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int square(int n)
{
int sq_n=0,odd=1;
for(int i=1;i<=n;i++)
{
sq_n+=odd;
odd=odd+2;
}
return sq_n;
}
int main() {

cout<<(square(16))<<endl;
return 0;
}

- Anonymous September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Straight forward version

public static int square(int value) {
		return square(value, value);
	}
	
	public static int square(int value, int multiplier) {

		if (multiplier == 1) {
			return value;
		}
		
		if (multiplier % 2 == 0) 
			return square (value << 1, multiplier / 2);
		
		return square(value << 1, multiplier / 2) + value;
	}

- frankierock September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

They're looking for O(log(value)) performance. Here's recursive and iterative versions.

public static int square1(int value) {
		int multiplier = value;
		int original = value;
		boolean odd = value % 2 != 0;
		
		while (multiplier > 1) {
			value <<= 1;
			multiplier /= 2;
		}

		return odd ? value + original : value;
	}

	public static int square(int value) {
		return square(value, value);
	}
	
	public static int square(int value, int multiplier) {

		if (multiplier == 1) {
			return value;
		}
		
		if (multiplier % 2 == 0) 
			return square (value << 1, multiplier / 2);
		
		return square(value << 1, multiplier / 2) + value;
	}

- frankierock September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be solved completely using bitwise operators in O(log(value))

unsigned long long square(unsigned n) {
  unsigned x;
  unsigned char bit;
  unsigned long long result=0;
  for(x=n, bit=0 ; x ; x>>=1, bit++) {
    if(x&0x1) result += n<<bit; 
  }
  return result;
}

int main() {
  unsigned number=9;
  printf("Sq(%d)=%u\n",number,square(number));
  return 0;
}

- Toseef September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its a very simple one. My Answer is

import java.util.*;
class Square
{
public static void main(String []s)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the value to find its square");
int num=scan.nextInt();
// Print the square of the number
int square=0;
for(int i=0;i<num;i++)
{
square+=num;
}
System.out.println("Square is"+square);
}
}

- Arihant jain September 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int n = 4;
int result = 0;
for (int i=0; i<n; i++)
{
result = result + n;
}

System.out.println(result);

- D September 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If this is double/float

double sq(double input){
if(input!=0){
return exp(2.*log(fabs(input)));
}else{
return 0}
}

- If not int January 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If this is double/float

double sq(double input){
if(input!=0){
return exp(2.*log(fabs(input)));
}else{
return 0}
}

- If not int January 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void Sqaure(int number){
     
     int square =0;
     
     for ( int i =0 ; i< number; i++) {
    square = square + number ; 
     
   }
     System.out.println(square);
 }

- hits January 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int square_num(int num)
{
    int square_val=0;
    if (num == 1 || num == 0) {
        return num;
    }
    // Check for negative number and convert it to positive
    if (num < 0) {
        num = num - num - num;
    }
    // Lets square the number
    for (int i=0; i < num; i++) {
        square_val = square_val + num;
    }
    return square_val;
}

int main()
{
    int num, square_val=0;
    cout << "Enter a number to be squared: ";
    cin >> num;
    square_val=square_num(num);
    cout << "Square of the the number "<< num << " is= " << square_val << endl;
    return 0;
}

- piyush1911 February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int main() {
	int a;
	int b;

	// number = a ^ b 
	cin >> a;
	cin >> b;

	double sum = 0;
	for (int i = 1; i <= b; i++)
		sum += log(a);

	cout << exp(sum);
	return 0;
}

- Shalyapin April 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int main() {
	int a;
	int b;

	// number = a ^ b 
	cin >> a;
	cin >> b;

	double sum = 0;
	for (int i = 1; i <= b; i++)
		sum += log(a);

	cout << exp(sum);
	return 0;
}

- Shalyapin April 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@import Foundation;

int squareNum(int num){
int answer = num = num > 0 ? num : -num;
for(int i = 1; i < num; i++)
answer = answer + num;
return answer;
}

int main() {
@autoreleasepool {
NSLog(@"%d", squareNum(-3));
} return 0;
}

- Anonymous June 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def square(a):
	if a<0:	
	    b=-a
	else:
	    b=a
	c=0
	while(b>0):
		c += a; b-=1
		
	if a<0:
		c = -c
	return c
	
	
print(square(2))
print(square(24))
print(square(-24))
print(square(0))

- simone.mapelli August 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-(NSInteger)square:(NSInteger)number {
    NSInteger result = 0;
    for (NSInteger i=0; i<ABS(number); i++) {
        result+=ABS(number);
    }
    return result;
}

- Anonymous December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-(NSInteger)square:(NSInteger)number {
    NSInteger result = 0;
    NSInteger firstNumber = ABS(number);
    NSInteger secondNumber = ABS(number);
    
    while (firstNumber) {
        if (firstNumber&1) {
            result+=secondNumber;
        }
        firstNumber>>=1;
        secondNumber<<=1;
    }
    return result;
}

- J December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Python and cost is o(log2(n))

def square(x):
		if x<0:
			return square(-x)
		else:
			return mul(x,x)
		
	def mul(x, y):
		if y==1:
			return x
		elif y==0:
			return 0
		else:
Return mul(x,y>>1)<<1+mul(x,y-(y>>1)<<1)

- zhangtemplar May 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int square(int n)
{
int sq = 0;
int in = n;
while(n)
{
sq += in << (int)(log2(n & (-n)));
n = n ^ (n & (-n));
}
printf("Square of %d = %d \n", in,sq);
return sq;
}

- chinmay April 09, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this should be a one liner

int
main ()
{
int i =5;

print (" twice of i is %d", (i<<1));
return (i<<1);
}

- Anonymous September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

If you remember your Kindergarten math, m * m is nothing but m summed up m times

- Jules Verne August 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you do better than O(m)?

- Anonymous August 30, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More