## Apple Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

Solution
Since query(i, j) gets frequently called, it should run as fast as possible, ideally in O(1) time.
I. When there is 0 between i and j, the query returns 0.
To quickly find out 0 between i and j, initial another array to keep track of at the current index i, where is the closest 0 to the left of i (including i).

II. To get the product of any given range without zero in between, build an array of cumulative products from the first positive number in the non-zero part up to current idx i. So that if there is no zero in between, query(i, j) equals to product[j] / product[i - 1] (O(1) time).
e.g. For A = [2, 2, 3, 4, 0, 4, 5, 6]
product array P = [2, 4, 12, 48 ,0, 4, 20 ,120]
query(1, 3) = P[3] / P[1 - 1] = 48 / 2 = 24

Remember to discuss with the interviewer if the product can get integer overflow

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

``````public class MultiplicationQueries {
int[] array;
int[] zeroToLeft;
int[] product;

public MultiplicationQueries(int[] a) {
array = a;
if(array.length > 0) {
zeroToLeft = new int[array.length];
product = new int[array.length];
zeroToLeft[0] = array[0] == 0 ? 0: -1;
product[0] = array[0] == 0 ? 0: array[0];
}
for(int i = 1; i < array.length; i++) {
zeroToLeft[i] = array[i] == 0 ? i: zeroToLeft[i - 1];
product[i] = array[i] == 0 ? 0: array[i - 1] == 0 ? array[i]: array[i] * product[i - 1];
}
}

public int query(int i, int j) {
if(i > j || i >= array.length || j >= array.length || i < 0 || j < 0) return -1;
if(i == j) return array[i];
return zeroToLeft[j] >= i ? 0: (i == 0 || array[i - 1] == 0) ? product[j]: product[j] / product[i - 1];
}
}``````

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

Solution to problem number 2 in python.

``````def flatten(seq):
r = []
for x in seq:
if type(x) == list:
r += flatten(x)
else:
r.append(x)
return r``````

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

product Ai....Aj

``````/*Apple Map Team
1. Given an array A and some queries, query(i, j) returns the result of Ai*...*Aj, in other words the multiplication from Ai to Aj.
The numbers in A are non-negative.
Implement query(i, j).
*/
public class RetrunResultOfAiAj {

public static int findProd(int i,int j, int[] a){
if(a==null){
return 0;
}
if(i>a.length-1 || j>a.length-1 || i<0 || j<0){
return 0;
}
//		if(i==j){
//			return a[i]*a[j];
//		}
int m = j-i+1;
int product = 1;
if(m>=1){
product = a[i] * findProd(i+1,j,a);
}
return product;
}
public static void main(String[] args){
int a[] = {1,2,3,4,5,6,7};
int b[] = {1,2,3,0,5,6,7};
int c[] = {1};
int d[] = {};
int e[] = {1,1,1,1,1,1,0};
System.out.println(RetrunResultOfAiAj.findProd(1, 4, a));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, b));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, c));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, d));
System.out.println(RetrunResultOfAiAj.findProd(1, 4, e));

System.out.println(RetrunResultOfAiAj.findProd(1, 14, b));
//		System.out.println(RetrunResultOfAiAj.findProd(1, 1, b)); wont work if i and j is same.
}
}``````

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

``````public static int findProd(int i,int j, int[] a){
int product=1;

if(i<0)return 0;
if(j>a.length)return 0;

for(int k=i;k<=j;k++){
product*=a[k];
}

return product;
}``````

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.

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

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