Interview Question


Country: United States




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

The best way in which I think it can be done is by first taking out all the elements in a separate vector which satisfy the condition. Then removing all the elements from the given Vector. Later adding all the elements back into the given vector.

- vermashubhang June 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple recursion solution is:

public Vector<Integer> eraseOddNumber(Vector<Integer> v) {
return eraseOddNumberHelper(v, 0);
}

private Vector<Integer> eraseOddNumberHelper(Vector<Integer> v, int ndx) {
if (v == null || ndx >= v.size()) {
return v;
}

if (v.get(ndx) % 2 == 1) {
v.remove(ndx);
} else {
ndx++;
}
return eraseOddNumberHelper(v, ndx);
}

- Anonymous June 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

vector<int> removeOddNums(vector<int> vec) {
    for (int i = 0; i < vec.size();) {
        if (vec[i] % 2 == 1) {
            vec.erase(vec.begin() + i);
        } else {
            ++i;
        }
    }
    return vec;
}

- Anonymous June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

That solution it's not nice because every time you erase an element you have to move all the next ones ones place to the left, so it's a quadratic algorithm. The best approach I think would be to do something like the partition of Quicksort, and then when you are finished you resize the vector to hold only the even numbers. (Linear, in-place solution).
If it's not required for you to do it in place the easiest way it's to count the number of even elements, create a vector of exactly that size and copy them from the original array.
Also in your solution you are passing a vector by copy (that's already O(n) time and extra space you are consuming). Consider passing a pointer or using a reference instead.

- gastonbengolea June 13, 2014 | Flag


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