Bloomberg LP Interview Question for Software Engineer / Developers






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

Following is working fine, tested in MSVC++ 6.0, debugged and noticed different addresses in the objects array.

// our class
class nodefaultctor
{
	int data;
public:
	nodefaultctor(int i)
	{
		data = i;
	}
};
// driver program
int main(int argc, char* argv[])
{
	nodefaultctor* obj[500];
	for (unsigned int z = 0; z < 500; z++) 
	{
		obj[z] = new nodefaultctor(0);
	}
	return 0;
}

- Rajendra Kumar Uppal February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Method 1:
---------
include <vector>
....
std::vector<class_name> object_name(500, class_name(value));
....

Method 2:
---------
class_name object_name[500] = {class_name(value), class_name(value), .......}
.

- Anonymous October 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or simply,
Method 1:
---------
include <vector>
....
std::vector<class_name> object_name(500, value);
....

- shiv November 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

so basically you want to make 500 objects right? Irrespective of any constructor or not?

- opex October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class obj_array[] = {1,2,3,.........}

- Erik October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IT is not possible to declare since it is necessary to have default constructor even u might need to call non default constructor

- DESTINY October 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use pointers?

- anyone October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

like this:

class (*object)[500];

- anyone October 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its simple, we first need to ask a question:
Whats integer vaue should I pass to constructor?
If the ans is support 10,

Objhect *obj = new Object[100] (10);

If the argument has to be decided at run time, then you cannot create an array of sucb a class directly.
create a vector of that Obj* and keep pushing obj* into it.

- Anonymous November 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the first approach is wrong.
It gets error:
error: ISO C++ forbids initialization in array new

- JS January 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

class Sample
{
private:
int pV;

public:
Sample(int i)
{
pV = i;
}
};

int main()
{
Sample ob[3] = { 0, 0, 0};

Sample *ptrSample = new Sample[100](5);

return 0;
}

- veky November 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesnt work dude.
If you dont have a default constructor, you can never initialize a static/ dynamic array/vector (u can use a single argument in vector though)

- watever November 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are two to create the objects:
TestClass objects[500] = {1,2,3,...,500};
but this is not possible if the number is that high.. so there is another way to create the object...
TestClass* objects[500];
Now you have 500 pointer to the TestClass objects. now initialize the pointer like -
while (i<500) {
objects[i] = new TestClass(0);
i++;
}
You can initialize object with the desired value or can change the value later.
Now with this approach you have to take care while de-allocating the memory.

- kk November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I certify your answer.

- Anonymous December 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

kai saare ladke heere hain !!!

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

If we provide one argument constructor, we "HAVE TO" provide the default constructor too.
Other wise the code wont compile at all.

To declare array of objects, you can do this, Assume 'Alfa' is class name

1# Alfa *a[10]; // array of 10 pointer to Alfa class
for ( i=0; i<10; i++)
a[i] = new Alfa(i); //call what ever the constructor you want.

2# Alfa a[10]; //will give 10 objects on stack.

- Anonymous November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can do it using double pointers :

TestClass **ptr;
ptr = new TestClass *[500];

for each i
ptr[i] = new TestClass("value");

- Anonymous November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe this is a correct answer, except you need to pass in an int value to TestClass(). Also you will need to ensure that memory is freed appropriately when done!

- colingaj December 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was asked this yesterday.

One trick to remember (and I don't see it here): Make constructor that takes int argument explicit.
class TestClass {
explicit TestClass(int size) { _ptr = new TestClass[size]; }
get() const { return _ptr; }
};

Then, do this:
TestClass a(500);

But what I didnt like abt my solution is the new guy _ptr.

- Nix November 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not working. The compiler still complains "no default constructor"

- Anonymous November 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer given by shiv is correct. :)
Thanks

- Altruist December 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why cannt we use malloc , malloc doesnt invoke a constructor .

class Array{

Array(int);
}



Array *a;

a = (Array *)malloc(sizeof(Array) * 500);

- sachin323 December 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is working!

- Mike January 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Sample
{
int i;
Sample(int k){i=k;}
}

void createObjects(int 500)
{
Sample* sample[500];
while(int i<500)
{
sample[i] = new Sample(i);
i++;
}
}

- Naren Narayana March 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

firewithin and kk's solutions are right but do not scale to a large number such as 500. A better way is to allocate just one large raw chunk of memory and use placement new to initialize all the objects. The following program initializes 500 objects using integer values 1 to 500.

struct A { A(int i) {} };

int main(void) {
const int N = 500;
A * ptr = static_cast<A *>(::operator new (sizeof(A) * N));
for(int i = 0;i < N; ++i)
{
new (ptr + i) A(i); // placement new
}
// Don't forget to call destructors at the end.
for(int i = 0;i < N; ++i)
{
(ptr + i)->~A();
}
// free memory.
::operator delete (ptr);
}

- sam July 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class base
{
public:
base(int x)
{
cout<<"init:base"<<endl;
y = x;
}
private:
int y ;
};

int main()
{
base b[500] = 1;

}

- nagaraj May 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

TestClass test[2] = { TestClass(10),
TestClass(20)};

- firewithin November 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

check your answers before posting rubish

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

firewithin's solution works!

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

Ladka Heera hai !!!

- Anonymous January 23, 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