## NVIDIA Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**Written Test

First of all you've got a mistake in result.

(2,6,4)/3 = 4. But you wrote 3.

it's not an optimal solution by space complexity, but works in O(N).

```
int []a = new int[]{2,6,4,2,3};
int N = 3;
int[] sum = new int[a.length];
int[] avg = new int[a.length];
for(int i = 0;i < a.length;i++) {
if(i > 0)
sum[i] += sum[i - 1];
sum[i] += a[i];
if(i >= N) {
avg[i] = (sum[i] - sum[i - N])/ N;
} else {
avg[i] = sum[i] / (i + 1);
}
System.out.println(avg[i]);
}
}
```

Rough idea: O(1) space, linear time

a -> input array

N -> input length for moving average (assume N is valid size)

Someone compile and test it for me ?

- S O U N D W A V E October 04, 2013