Bloomberg LP Interview Question for Software Engineer / Developers






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

use a smart pointer.

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

use initializer list and smart ptrs

- NewStart April 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use auto_ptr<> for auto delete if there is exception.

class B {
int *p;
public:
B(){
auto_ptr<int> ap(new int);
// Do something with potential exception
p=ap.release();
}
};

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

What if I simply do this,


class A{
int* x;
int* y;
int* z;
public:
A(){

try{
x = new int(10);
y = new int(11);
z = new int(12);
}catch(...){
delete x;
delete y;
delete z;
}

}//end of A constructor

~A(){
delete x;
delete y;
delete z;
}
};

- Patron April 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you cant do like this.
The better way is to put each new inside nested try catch blocks. If there is an exception in allocating memory for x then doing what you did would try to delete y and z which are not allocated when x is getting allocated.

try {
x = new int(10);
try{
y = new int(11);
}
catch(...)
{
delete x;
delete y;
}
}
catch(...)
{
delete x;
}
}

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

My bad ....Thanks for correcting me.....

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

Remember to rethrow the exception. Otherwise the program will think that the object has been constructed properly.

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

Guys, The only way to do this is by using the so called C++ construct of Function try blocks. Read the concept of function try blocks on the following page: www dot physics dot irfu dot se / aCCdoc / except.htm

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

duplicate Q. Ans: By using function try-blocks. The key word here is 'function'

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

Read this: gotw[dot]ca/gotw/066[dot]htm

They have explained why do you need function try blocks for constructors. Somewhere in that article.. it says if exception occurs in function try block then whatever objects that were successfully created will be destructed by calling their destructors.

// Example 1(a): Constructor function-try-block
//
C::C()
try
  : A ( /*...*/ ) // optional initialization list
  , b_( /*...*/ )
{
}
catch( ... )
{
  // We get here if either A::A() or B::B() throws.

  // If A::A() succeeds and then B::B() throws, the
  // language guarantees that A::~A() will be called
  // to destroy the already-created A base subobject
  // before control reaches this catch block.
}

- Anonymous April 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good example! Thanks a lot.

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

through smart pointers

- Anonymous May 05, 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