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

Comment hidden because of low score. Click to expand.
0

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

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

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

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

}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is wronggg....

Comment hidden because of low score. Click to expand.
0

Why this is wrong? It seems right...

Comment hidden because of low score. Click to expand.
0

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

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

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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

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``````

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 ?

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

Comment hidden because of low score. Click to expand.
0

3 lines are enough

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...).

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

Ugh. 0 votes for this gem.

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)

Comment hidden because of low score. Click to expand.
0

Ugh.

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

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)

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

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

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

num/(1/num)

Comment hidden because of low score. Click to expand.
0

not define for 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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))``````

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

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

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)``````

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

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

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

Can you do better than O(m)?

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.

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