## Apple Interview Question for Software Engineer / Developers

Country: United States
Interview Type: In-Person

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

Does this make sense?

``````int square(int num)
{
int result = 0;

int n = (num > 0) ? num : -num;

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

return (num % 2 == 0) ? result : -result;
}``````

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

sorry, the return clause should be
return (num % 2 == 1 && num < 0) ? -result : result;

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

Why is the last sentence judge whether num%2==1&&num<0? How could it return negative number? Can you give example?

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

See this duplicate where a better solution has been posted: /question?id=5748016728244224

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

int square(int num)
{
int i,result=0;
for(i=0;i<num;i++)
{
result=result+num;
}
return result;
}

Comment hidden because of low score. Click to expand.
0
of 0 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
of 0 votes

clearly says without using "*"

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

Here is the approach to do in n/2 loop for a num 'n':

``````int square(int num)
{
int i,x,sum=0;
x = num/2;
for (i=0;i<x;i++)
{
sum = sum+num;
}
sum = sum+sum;
if(num%2)
sum=sum+num;

return sum;
}``````

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

int Square(int num)
{
int counter = 0;
int val = num;
int sum = 0;

while(val > 0)
{
if(val & 1 == 1)
{
sum += num << counter;
}
counter++;
val >>= 1;
}

return sum;
}

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

public class App {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}

}

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

``````public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}

}``````

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

``````and
public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}

}``````

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

``````public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}``````

}

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

``````public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}``````

}

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

``````public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}

}``````

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

``````public class App {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));

}

}``````

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

``````Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number to find its square: ");
int num = scanner.nextInt();
scanner.close();
int squaredVal = 0;
for(int i=0; i<Math.abs(num); i++){
squaredVal = squaredVal + num;
}
System.out.println("Squared value is " + Math.abs(squaredVal));``````

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

Any number can be expressed as sum of powers of 2.
n = 2**k + 2**l + ...
using that principle square(n) = n*(2**k) + n*(2**l) + ...
=(n << k) + (n<<l) + ...
code below

``````int getsquareNoAsterisk (int n) {
int sn= 0;
int pos = 0;
int curN = n;
while (curN != 0) {
if ( (curN & 1) == 1 ) {
// This number has 2**pos value so add it with mulitplied
// by same number using << operator
sn += (n << pos);
}
curN >>= 1;
pos++;
}

return sn;
}``````

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