NVIDIA Interview Question for Software Engineer / Developers


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 ?

- S O U N D W A V E October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous October 21, 2013 | Flag
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]);
          }
    }

- glebstepanov1992 October 04, 2013 | Flag Reply
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);
}

- Anonymous October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- anuraj December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous July 06, 2014 | Flag Reply


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