Apple Interview Question for Developer Program Engineers






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

// Return value overflows for large x and x ~ 0
double Square(double x) {
  return x / (1.0 / x);
}

- thomas November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome.. tricky answer

- different anonymous November 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Add original number the same num of times as the value of orig. Num. So square of 6 will be adding 6, 6 times.

- Anonymous December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

for (int i=0 ;i<n; i++)
  square+=n;

- anon July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

every number can be written as a sum of 2s and 1.
like 5 = 2+2+1, 6 = 2+2+2
So 5*5 = 5*(2+2+1) = 5*2 + 5*2 + 5
Use left shift for *2 as asterisk is not allowed. then add num for odd.

- Anonymous November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey28074" class="run-this">public class BitwiseSquare {

/**
* Simple multiplication using bitwise operators.
*/
public static int multiply(int a, int b) {
int product = 0;

while (a > 0) {
if ((a & 1) != 0)
product += b;
b = b << 1;
a = a >> 1;
}
return product;
}

/**
* This is better explained using an example. Example: 24 = 16+8 (bit 4th
* and 3rd) 24^2 = (16+8)^2 = 16^2 + 8^2 + 2x16x8 26^2 = (16+8+2)^2, using
* powers of 2. (16+8+2)^2 = 2*2+2 + 8*8+2*8*(2) + 16*16+2*16*(2+8) = 676
*/
public static int square(int a) {
int bitpos = 0;
int result0 = 0; // 16*16 + 8*8 + 2*2
int result1 = 0; // 2 + 2*8*(2) + 2x16x(2+8)
int temp = 0; // 2+8+16

while (a > 0) {
if ((a & 1) != 0) {
// 16^2 = (2^4)^2 = 2^(4+4)
int b = 1;
// b = 16 = 2^4
b = b << bitpos;
// result1 += 2 * b * temp;
result1 += multiply(b, temp) << 1;
temp += b;

// b = 16^2 = 2^(4+4)
b = b << bitpos;
result0 += b;
}
a = a >> 1;
bitpos++;
}

return result0 + result1;
}

public static void main(String[] args) throws java.lang.Exception {
for (String s : args) {
int val = Integer.parseInt(s);
System.out.println(val + "^2 = " + square(val));
}
}
}
</pre><pre title="CodeMonkey28074" input="yes">1 3 26</pre>

- jasantunes November 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not just call your multiply () with both a & b same i.e multiplication of a & b where a=b?

- Anonymous December 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Indeed! :)

- jasantunes December 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

another solution:

Use the fact that (n-1)^2 = n^2 -2n + 1 and use recursion

{
int square(int n)
{
  if(n == 1 || n == 0) return n;
  if(n < 0) return square(-n);

  return square(n-1)+n+(n-1);
}

- Anonymous December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks best solution to me

- coolGuy January 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

original question states a number not an int, how about doubles?

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

What the heck is a carrot sign?

- noname.c August 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is a carrot: ^

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

LOL

- noname.c October 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Caret!!! I die a little inside when I read such misspellings

- Anonymous November 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int result;
result = sqrtnum(100);
Console.WriteLine("The value is " + result);
Console.Read();

}
public static int sqrtnum(int num)
{
int total = 0;
int count = num;
List<int> l = new List<int>();
for (int i = -1; i < count; i++)
{
l.Add(num);
Console.WriteLine(l.ToString());
}


for (int j = 0; j < l.Count - 1; j++)
total += l[j];
return total ;
}

}
}

- An Nguyen September 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(int)square:id
{
int a;
a=id;
for(int i=0;i<a;i++)
a=a+a;
return a;
}

- deepthi October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int square(int a, int b) {
if (b==1) return a;
return a+square(a,b-1);
}

- sankar October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A lot of answers are focusing on integer numbers only. I don't think it states anywhere that we should limit the function to integer numbers.

if we're allowed to use <math.h> you can do:

double square(double x) {
	return exp(log(x) + log(x));
}

a^b = exp(b*log(a))

- Nils Hayat October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nils Hayat: how a*a = exp(b*log(a)) ?Care to explain more?

- anish October 14, 2013 | 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