Goldman Sachs Interview Question for Software Engineer / Developers






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

The C++ Standard says that an STL element must be "copy-constructible" and "assignable." These fancy words simply mean you can safely assign or copy one object to another and get two independent and logically identical copies. In particular, the state of the original object shouldn't change when it is copied to the target object.

This is not the case with auto_ptr, though: copying or assigning one auto_ptr to another makes changes to the original, in addition to the obvious changes to the copy. To be more specific, the original object transfers ownership of the pointer to the target, and the pointer in the original object becomes null. Imagine what would happen if you did something like this:


class Foo{};
vector < auto_ptr <Foo> > vf; //a vector of auto_ptr's
//..fill vf
int g()
{
auto_ptr <Foo> temp = vf[0]; // vf[0] becomes null
}


When temp is initialized, the member vf[0] is changed: its pointer becomes null. Any attempt to use that element will cause a runtime crash. This situation is likely to occur whenever you copy an element from the container. Remember that even if your code does not perform any explicit copy or assignment operations, many algorithms (swap(), random_shuffle(), sort(), and many others) create a temporary copy of one or more container elements. Furthermore, certain member functions of the container may create a temporary copy of one or more elements, thereby nullifying them. Any subsequent attempt to the container elements is therefore undefined.

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

Very Nice Explanation.

- Anonymous May 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

auto_ptr does not copy well. For this reason, putting them in containers like vectors, queues is not recommended.

- siacca November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would also add here that boost::shared_ptr<T> is much better for this purpose.

- R.Kozikowski January 26, 2011 | 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