## Apple Interview Question

Software Engineers**Country:**United States

**Interview Type:**In-Person

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

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

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

- aonecoding July 25, 2017