Bloomberg LP Interview Question for Software Engineer / Developers






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

if we are decreasing the size of vector then there is no need to reallocate the vector.
If we are increasing the size of vector and there is no contiguous memory then vector won't get allocated ... it will return error.....

- tito March 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

IF we are decreasing the size of vector, or if we are increasing the size but the vector still has enough capacity, there's no need to allocate memory. When capacity is not enough, the function will cause a reallocation: 1). allocate contiguous memory elsewhere, which can hold the original vector plus new elements. if such a space is not available, return false; 2) copy the original vector and then reclaim the space; 3) add new elements at the back.

- Vivi April 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, when reallocation is not successful, this function will not return a false, it's a void function. Instead, a bad_alloc exception will be thrown.

- Vivi April 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

vivi,
so do you mean if contigeous memory is not available then vector resize function throw errors ?

- keyur April 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Vivi, good answer!
keyur, When there is no space to allocate a contiguous block of the required size, it probably means "memory exhaustion". Throwing an exception is the right thing to do in this situation.

- Anonymous June 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Memory is allocated some where else(if it is available) and the contents of this vector are copied into that memory. Memory allocation depends on two strategies "first fit" or "best fit". Don't forget that java is build up on "C".

- vikram rathode April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the declaration of resize:
void resize(size_type sz, T c = T());

if sz < vector's size then resize should have this effect:
erase(begin()+sz,end());
if sz > vector's size then resize should have this effect:
insert(end(), sz-size(), c);
if the size is equal, then nothing is done.

Note that I am not telling that your implementation must call either erase or insert, but it must behave like...

- Anonymous May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Vikram is right. It allocates memory some where else if it can find contiguous memory and copy the entire content. If memory is not available at all then exception is thrown

- Karthik Raj June 19, 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