Google Interview Question for Software Engineer Interns


Country: Brazil
Interview Type: In-Person




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

Vector is implemented base on array, say size m.
1. If the array is full, create a new array of size 2m, copy the elements from original array to the new one, and put the to-be-inserted element into the new array
2. The size gets doubled.
3. If the array has size 0 for initialization, then you need log n copies to insert n elements.

- yangxh92 November 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. this question is about out of bound read/writes. For instance, the size is currently 8 and you use 'vector[9] = 1'.

2. correct.

3. logn is the number of reallocations, not number of copies.

- Victor November 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Victor, I think author means that if the size of the vector hits to the capacity, We will immediately increase it to double, instead of when user writes vector[9]. In this way you will never have to be reaching out of bounds.

- nonameno February 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question 1 is really about out of bounds access, I was the person interviewed. Most of the big languages do not grow the vector until an access becomes inbounds, if you think about it. In C++ this leads to an invalid read of memory, and in Java an exception is thrown.

- Victor February 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Initial we can start with size of the array as 10 or more than that.
If it reaches boundary we can double it or we can increase by 1.5 times.
we have to copy all the elements from the old array to new array.
We can maintain a variable which contains the number of elements presently array holding

- new November 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Considering the size of the Vector gets doubled, every time it exceeds the current size of the array, then the number of copies performed by the vector to insert 'n' elements would be:

Lets say, initial size of Vector is 'm', and number of elements inserted in the Vector totally is 'n', then number of copies = n/m.

For e.g, if initial size of Vector is 3, and 9 elements were inserted, then in total, the number of times the copy() function was performed is 3.

Is this interpretation correct?

- Sweta Shah January 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Templatized container class holding pointer to a data and a nested iterator using operator overloading.

- Rathi April 27, 2015 | 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