Goldman Sachs Interview Question for Developer Program Engineers






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

Sample s[5] = {1,2,3,4,5};

- Nageswar May 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is right answer. The values 1,2,3,4,5 are supplied to the constructors of s[0],s[1],s[2],s[3] and s[4]

- Sri June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

There are multiple ways.
1. Use vector STL.

class A
{
        int p;
        public:
        A(int i)
        {
                p=i;
                cout<<"Called"<<endl;
        }
        A(const A & rhs) :p(rhs.p) {
                cout << "Copied\n";
        }
 
};
 
int main()
{
        vector<A> v(5,A(1));<-------5 objects are constructed.
        return 0;
}

output here: ideone.com/lHX5W

2. Call constructor for each object.
A arr[3]={A(1),A(2),A(3)};
This would be better if we want to initialize different objects with different values.
The first object is initialized with1, second with 2 & so on.

However,in the first method using vector, only one object is constructed & initialized with 1 [in our case], & copy constructor is called 5 times to copy that object.

- Aashish July 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

If you call new, then there will be a constructor call which will search for the default constructor. And, since there is no default constructor and the compiler can't create it because of presence of a parametrized constructor, it will report error.

So, the best you is to allocate the memory C-style and then initialize it.

class A{
public:
int a;
A(int b): a(b){}
};

int main()
{
 A *a1,a2(1);
 a1 = (A*)malloc(sizeof(A)*10); // equivalent to A[10]. 
 for(int i=0;i<10;i++) a1[i]=a2; // Initialization is important in any program.
 for(int i=0;i<10;i++) cout<<a1[i].a;
 return 0;
}

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

this is the correct answer, above answers are partially correct. most probably interview is looking for answer given by maX

- Sirius April 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

A a[10] = {A(0), A(0), A(0), A(0), A(0), A(0), A(0), A(0), A(0), A(0)};

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

Thats correct

- nik February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats correct

- nik February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- siva.sai.2020 May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

class TryIt {
	int aa;
	public TryIt(int a) {
		aa=a;
	}
}
public class Misc1 {

	
	public static void main(String [] args) {
		TryIt [] arr ={new TryIt(1),new TryIt(2)};
		
		System.out.println("arr "+arr[0].aa);
	}
}

- najaf.zaidi January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A a[10] = new A[10];
for( int i=0; i<10; i++)
{
a[i] = new A( i ); // initialized to i;
}

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

This isn't Java, son.

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

WTF this is absolutely java

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

operator new + placement new. Times needed IRL: 0 (use a vector, kids).

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

Can you elaborate...

- Test May 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<new>
using namespace std;
class A{
public:
A(int i)
{
k=i;
}
~A(){};
void fun();
private:
int k;
};

class B{};

int main()
{
void *ptr= operator new[](10*sizeof(A));
A *p=static_cast<A*>(ptr);

for (int i=0; i<10; ++i)
{
new(&p[i]) A(i);
}

for(int i=9; i>=0; --i)
{
p[i].~A();
}
operator delete [] (p);
return 0;
}

- Nest May 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try 2D, like

A **a = new A*[10];
for (int i=0; i< 10; i++) {
a[i] = new A(0);
}

- sol May 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let class A be:
class A{
int a;
public A(int a){
this.a = a;
}
}

so an array of this class can be initialized as:
A[] a = new A[10];

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

This is not a new question..Sol's got the right one though...from Myers.. :)

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

B.T.W..comment on Sol's is by Prem.

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

why this code perfectly running and giving ouput 56

int main()
{
int *p=malloc(0);
*p=56;
printf("%d",*p);
return 0;
}

- kamal June 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Java solution

private A[ ] a;

public A(int i) {
if( a == null) {
System.out.println("executing first time" + i);
a = new A[i];
}
if(i <= 0) {
// done with initialization
} else {
System.out.println("creating object A ..." + i);
a[--i] = new A(i); // creates 'i' number of 'A' objects
}
}

- Anonymous February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Java solution

private A[ ] a;

public A(int i) {
if( a == null) {
System.out.println("executing first time" + i);
a = new A[i];
}
if(i <= 0) {
// done with initialization
} else {
System.out.println("creating object A ..." + i);
a[--i] = new A(i); // creates 'i' number of 'A' objects
}
}

- taojfew February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Java solution

private A[ ] a;

public A(int i) {
if( a == null) {
System.out.println("executing first time" + i);
a = new A[i];
}
if(i <= 0) {
// done with initialization
} else {
System.out.println("creating object A ..." + i);
a[--i] = new A(i); // creates 'i' number of 'A' objects
}
}

- taojfew February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Java solution

private A[ ] a;

public A(int i) {
if( a == null) {
System.out.println("executing first time" + i);
a = new A[i];
}
if(i <= 0) {
// done with initialization
} else {
System.out.println("creating object A ..." + i);
a[--i] = new A(i); // creates 'i' number of 'A' objects
}
}

- taojfew February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following solution works :

class B 
{
private :

	int i ;
public :
	
	/*B()
	{
		i = 0 ;
	}*/
	 B(int d)
	{
		i = d ;
		cout<<"const " <<d<<endl;
	}


} ;

int main ()
{
	B b = 1;
	B b1[2]= {1};

	B *ptr[2] = {new B(3) };
    cout << ptr[0]<<endl;
  cout << ptr[1]<<endl;

	return 0;
}

- Anonymous February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

u cannot initialize an array without having a defualt constructor

A a[10] = new A[10]; will give compile error

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

why not?
You can not do
A a[10]
but you can do
A a[] = {A(1), A(2)}

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