Goldman Sachs Interview Question for Developer Program Engineers






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

1)make the destructor as a private member function
2)add a public function which will have this statement :

delete this;

- pritesh.kumar2 June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think no need of step 2.
This will work.

#include <iostream>
using namespace std;
class A
{
	private:
		int a;
		~A(){cout<<"~A()\n";}
	public:
		A(){cout<<"A()\n";}
};
int main ()
{
	A *pa = new A;
	A a;
	return 0;
}

In main line 3 will give compilation error.
But here the problem is once the object is created on the stack, it cant be deleted.
Does somebody have a better technique.

- @above June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But here the problem is once the object is created on the HEAP, it cant be deleted.

- Correction for above line June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

slight modification to your code will do the job;

#include <iostream>
using namespace std;

class A{
  
    private:
    ~A(){ cout << " going in " << endl;}
    public:
     A(){ cout << going out " << endl;}
     void ClearA(){ delete this;}
};

int main()
{
  A *a = new A;
  a->ClearA();
  return 0;
}

- pritesh June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cool solutions!

Could you pls explain why it's not possible to create object of "class A" on stack when the destructor is declared as private? Thanks in advance.

- anon June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

all objects created on stack will be checked at compile time and since objects created on stack need to be destructed after a block it checks for all the required information before doing so.

Since destructor is in private it cannot see it.

- sandeep May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;

class A
{
public:
A();
private:
};

A::A()
{
cout<<"In constrcutor"<<endl;
}

int main()
{
A a;
A b;
cout<<"Address of a is"<<&a; // o/p is 0XXX0003a -> Means this was allocated on the Stack
cout<<"Address of b is"<<&b; // o/p is 0XXX0003b -> Means this was allocated on the Stack
A *c= new A();
cout<<"Address of c is"<<&c; // o/p is 0XXX00024 -> Means this was allocated on the Heap
}


#include<iostream>
using namespace std;

class A
{
public:
A();
private:
~A();
};

A::~A()
{
cout<<"Ïn destructor"<<endl;
}

A::A()
{
cout<<"In constrcutor"<<endl;
}

int main()
{
char buf[2]; --> Allocated on stack
A a; --> Throws an error because when the local object goes out of scope immidietely linker will call the destructor but here it is pvt
A *c=new(buf) A(); -> Allocated on the heap
delete c; --> throws an error bcz destructor is pvt
cout<<"Address of a is "<<&c<<endl; --> // o/p is 0XXX00024 -> Means this was allocated on the Heap(this is the heap address , see the above pgm
cout<<"Address of a is "<<&buf<<endl;
getchar();
return 0;
}

In order to release the memory we should have to write our own user define function. Any way we can't free the memory directly in C++ either we should have to call delete/user-defined function.

Hope this helps

- Rajesh Manem July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we write the private constructor in such case if we want that tha class cant be instantiated
dont talk vague

- ashutosh January 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

make the constructors private. This will restrict the creation of an object on the stack. The heap object however can be created by the new which can call the constructor.

- abhityagi85 October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above is a simplest case.
An advanced case may be to make the destructor private. As constructors may have different signatures while destructor's signature is unique, it will always be hepful.

- abhityagi85 May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you make constructor private, this means you are stopping anyone to create an instance of the class.

- nitin July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

its not true. I can still make object on stack by using:

#include <iostream>using namespace std;

class A{ private: ~A(){ cout << " going in " << endl;} public: A(){ cout << going out " << endl;} void ClearA(){ delete this;}};

int main(){ char buf[10000]; A *a = new (buf)A; return 0;}

buf is on stack which a is create on this address..

- Yino June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this solution can have portability problems related to alignment .

i think if we make the constructor also private and expose some static public instantiation methods , which will create the object using new ,then that solution is perfect .

- pritesh June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about keeping all the constructors as private and just giving the placement new operator as public.

- Birinder Singh June 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Pritesh suggestion is in line with the more traditional solution and is called the 'Named Constructor Idiom.'

Search the net for the C++ FAQ by Marshall Cline. Check out FAQ [16.21] which answers the following question.
"How can I force objects of my class to always be created via new rather than as locals or global/static objects?"

This technique is also commonly used with the 'Factory Pattern.'

- Developer July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

think its better to make the c-tor private and provide a static member to create the object and here, make sure its created on the heap...

class x {
public:
static x* create() {
return new X();
}

private:
x(){}
};

- Rv July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look at Named constructors idiom
parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.21

- Anonymous July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't delete this undefined?

- rrnit August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't Singleton class create object on the heap

1. Protected/Private Constructor
2. Static GetInstance method to initialize the object
3. public Destructor

public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() { }

/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.instance;
}
}

- Cyber Phoenix May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't Singleton class create object on the heap

1. Protected/Private Constructor
2. Static GetInstance method to initialize the object
3. public Destructor

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
 
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton instance = new Singleton();
        }
 
        public static Singleton getInstance() {
                return SingletonHolder.instance;
        }
}

- Cyber Phoenix May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

class A {
public:
	static A* getInstance() { return new A; }
	~A() { cout << "~A()" << endl; }
private:
	A() { cout << "A()" << endl; }
};
int main() {
	//A a;
	A * ptrA = A::getInstance();
	delete ptrA;
	getchar();
	return 0;
}

- Passerby_A February 24, 2014 | 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