Apple Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
How do you know when stop counting your negative numbers? you only have the number (2, 3, 4) ...
int squareNum(int num){
int answer = num;
for(int i = 1; i<num; i++){
answer = answer + num;
}
return answer;
}
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;
}
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
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);
}
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...).
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;
}
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;
}
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;
}
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;
}
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);
}
}
#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;
}
Sum of Odd numbers
- Abhay August 31, 20141*1 = 1 // 1
2*2 = 4 // 1+3
3*3 = 9 // 1+3+5
4*4 =16// 1+3+5+7
and so on