Google Interview Question for Software Engineer in Tests






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

Do you mean to optimize the algorithm to make the complexity less than O(n)?

Also the codes above seems wrong , it should be:
vector<int>::iterator i;
int count;
for (i = elements.begin(); i < elements.end(); i++) {
if ((*i) != 0) {
//count++ should be put here
count++;
if (count == n) {
return (*i);
}
}
}
return -1;
}

- zwfreesky April 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the array is very large, the following would be an improvement:

sort the array, but keep track of each of the elements' original indicies. All 0s will be at the beginning of the sorted array. Find the nth 0 by traversing the sorted array. Once you have that 0, you will have the index in the original array one less than that of the element you want to return. This has a best case complexity of O(logn). Worst case is 0(n) - if all but 1 element is a 0.

- anon April 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't read the question carefully enough - this improvement will not work.

- anon April 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One small improvement for large vectors would be to sort it, determine how many 0s exist in the vector (and where). Given this information, you could automatically offset the index to retrieve the element you need, given how many 0s exist in the vector.

- anon April 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use switch-case instead of if else to make the code faster.
I don't think we can make the performance better than O(n).

int getNthNonZero(vector<int>& v, const int n)
{
  int count = 0;
  vector<int> :: iterator i;
  for (i = v.begin(); i != v.end(); i++) {
    switch (*i) {
      case 0:
        break;
      default:
        switch (++count == n) {
          case false:
            break;
          default:
            return *i; 
        }//switch on count
      }//switch on *i
  }   
  return ERROR;
}

- Balaji April 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"We can use switch-case instead of if else to make the code faster."
wtf, how do say that? its just to make the life easier for the programmer and not the way you think. think twice before you post anything.

- LOLer August 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

U fucking cunt.
switch-case is faster than if-else.

http://sequence-points.blogspot.com/2007/10/why-is-switch-statement-faster-than-if.html

So, stop being a ridiculous snob.

- Balaji August 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL.

- The real LOLer August 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude, do you even look at the code that you are citing? This code has only two branches. If anything, your switch makes the code look ugly.

- Anonymous September 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple optimization would be to start from the end of the array if given n is greater than count/2.

- Metta April 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that's not gonna work. the number of zeros in in the first half is unknown

- lee.skywalker May 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could not do better than linear complexity, but there are a couple things I would do if it were C programming. Somebody more aware of C++ can be the judge.
+ Pointer deref is always costly w.r.t. cache perf. So, *i should be replaced if possible.
+ use post increment for count i.e. one access only.
+ remove comparison of *i to zero: reduces one cmp instruction
+ register keyword is a possibility: may screw up register optimization by compiler
+ if vector size is known, then compare n and size. If equal, then just check the last one to see if we should even go forward.
+ Test whether vector is empty

- sk_seeker April 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about start the check from nth?

- jack April 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is wrong.I thought it incorrectly.

- jack April 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about create another array contains all the non-zeros in the original order. then the rest is simple. Total cost is constructing the array: O(N), then the future cost is O(1)

- lee.skywalker May 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the problem with that is when there are additions to the vector. Then you'd have to check into both vectors (worstcase) and update one or both. That can be quite expensive.

- hgk August 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@lee.skywalker
the complexity remains same as the earlier working solution.

- googler August 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about if we remove count altogether? change the code to:
if ((*i) == 0) {
n++;
if (i== n) {
return (*i);
}
}
hoping changing 'n' doesn't impact otherwise, the complexity remains same, however we got away with 1 var and increment.

- Anonymous August 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Whatever you do, you will not gain much improvement with this design. You need to get rid of the search. If your life depends on it, just put the indexes of the zeroes into another vector while you are populating the elements vector; or better, abstract all this away by using a class to manage the data in elements vector.

- interviewtomorrow September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with sk_seeker. I believe the improvement comes from the way coding. I would avoid using iterator if possible if [] or index operation is allowed.

- aaaaaaaa December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

search_n_if(element.begin(), element.end(), n, bind2nd(not_equal_to<int>(), 0));

- tao January 13, 2010 | 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