Goldman Sachs Interview Question for Software Engineer / Developers






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

The major problem is the abuse of memset here. It erased the whole Foo object and it destroyed VPTR in Foo. So when you call virtual function, it will crash.

- xiaofeng.ustc January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will not crash if object created statically.

- ash.taunk3 September 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

on memset to Foo object f it's vptr also be set to NULL.which is stored in the object's starting address.

- Anurag February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried a little different version of the above code. And it ran smoothly, giving 10 and 0 as output.

class Foo
{
int a;
public :
Foo(){
a=10;
}

virtual int Fun1() { return a;};

};

class X
{
Foo f;
public:
X()
{
memset(&f,0x0,sizeof(f));
}

int Fun1() { return f.Fun1();};
};

int main()
{
Foo ob;
cout<<"\n"<<ob.Fun1();

X x;
cout<<"\n"<<x.Fun1()<<"\n";
return 0;
}

I think I preserved the logic of code given. Please correct me if I am wrong. I still don't understand what(if any) is the problem with that code.

- JohnyCigar February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ anurag - Agree with you but does it cause any harm? I don't think so.

@JohnyCigar - yep, code works w/o any problem. even I am still thinking what problem this snippet has. I don't see any issue in executing the program.


This Qn costed my exit in second round.Interviewer grilled me a lot around this qn.He seemed to be fascinated by this Qn :-(

- RS February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

try calling the virtual function after u declare obj X. the function pointer in vtable will be set to 0 and will reference a null, not the fun1 function

- nic March 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right, the problem with this code is that we're memsetting an object which has a virtual function.

- boboon April 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@JohnyCigar

- Divyang J. Mithaiwala April 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@JohnyCigar
your code works because still you have not used virtual functions fundamental. Which works on pointer or reference of object.

int Fun1()
    {
        Foo *p = &f;
        return p->Fun1();
    }

instead of

int Fun1() { return f.Fun1();};

your code crash with "Segmentation fault"

- Divyang J. Mithaiwala April 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for me the code crashes if I tried to use f in class X to access Fun1() of class Foo i.e. f.Fun1()

- Vineet February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Vinnet
Thanks, you are right code crashes when f try to access Fun1() since Fun1() is virtual and memset has already reset the vptr of f to Null however if we remove virtual keyword from Fun1() then code works fine.

- RS February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@RS & Vineet : When I compiled this code on VS it didn't crash. Virtual ptr concept will come if we are calling Fun1 with Fun object pointer. I modified code and changed Fun f to Fun *f.

If we try calling Fun1 now using ptr f, program will crash, since virtual pointer is set to null.

- Preeti March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My guess is(purely a guess) but Class X has Foo f. The constructor is X is setting the memory location of f to 0x0. This break encapsulation principle because if you look in your compiler, the value of the private variable a of class Foo gets set to 0. Am I wrong?

- anonymous March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I did not get any error with the original code. It is running fine.

- Richa April 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Getting 10 and 0.

- Dev Null April 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vptr gets created in both kind of objects (statically created or dynamically created)

class base{
    int i;
    virtual void show() {..}
};

1. Statically created:

base b;

2. Dynamically created:

base *b = new base();

Point is when you access any virtual function through these objects then vptr is used or not.
For case 1st - Vptr is not used. So even if vptr is NULL, still virtual function can be called, because there is no polymorphism.
For case 2nd Vptr is used. If Vptr is NULL , then calling to virtual function will give null pointer exception.

- ash.taunk3 September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

'a' in Foo is private, can not be set by derived class X.

- Anonymous February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

X contains Foo object here. it is not derived from Foo.
Why do you want to set a ?

- RS February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when the object of X class is getting initialized, the member of that class are initialized too, at that time default constructor of that f class is getting called, and after that with the memset statement we are going to access the private member, that's why it is not possible.

- sourav February 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just think about only one error tat is main does not return anything ........and object is not allocated......

- jagdish January 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

maa k lodon chup ho jao

- Anonymous October 05, 2012 | 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