## NVIDIA Interview Question for Software Engineer / Developers

• 1
of 1 vote

Country: United States
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
7
of 9 vote

Rough idea: O(1) space, linear time
a -> input array
N -> input length for moving average (assume N is valid size)

``````int sum=0;
int tail=0;  //trail an index (behind "i" by N)

for(int i=0; i < a.length; i++)
{
sum += a[i] - (i < N ? 0 : a[tail++]);
result[i] = sum/min(i+1, N);
}``````

Someone compile and test it for me ?

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

sum may increase int length in course , so prev_avg*(n-1)/n+a[nth_elem]/n

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

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

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

``````vector<int> inp, op;
int sum =0 , count =0;
for(vector<int>::const_iterator i = inp.begin();
i != inp.end();
++i) {
sum += *i;
++count;
op.push_back(sum/count);
}``````

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

result= a;
for(i = 1; i < N; i++)
{temp = result[i -1] * i;
result[i] = (( temp+ a[i])/ (i+1))
}
Not tested

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

Nth-avg = ((N-1)avg*(N-1)+Nth element)/N

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.