Cognzant Technology Solutions Interview Question for Software Analysts


Team: Embedded
Country: United States
Interview Type: In-Person




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

- For counting dynamically created objects we can use operator overloading technique

void * operator new(size_t sz)
{
// create the object
// increment the number of objects
}

similarly we have to use operator delete to know exactly the number of objects are alive.

I am not sure about to find the static number of objects. Generally we can use static counter to determine the number of objects(includes both static and dynamic).

- Rajesh Manem November 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does not handle the case when MyClass is a member of another class FooClass and FooClass is created on the heap

- Anonymous November 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Static objects can be determined as follows:

- Place the counter variable in constructor( it increments the objects for both static and dynamic)
- Subtract the dynamically allocated objects count

For example:
I created 10 static and 20 dynamic
then the counter = 30 ( as per constructor)
Subtract dynamic counter from counter gives you the number of static objects

Static = Counter - Dynamic Obj
Dynamic = will be determined by operator overloading

- Rajesh Manem November 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you count the number of dynamic objects to subtract

- bhawin@t9l.com December 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

At the constructor of the MyClass you can have counter (for both static and dynamic objects)

At the overloaded new operator, have another counter exclusive for dynamic allocations

Now subtract former with latter, you will have count of the number of static objects

- Avinash November 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

make a static Counter and increment it in all constructors even in copy constructor also so this will tell total no of objects now u need to override new new[] and placement version also and make a dynamic counter here also static type so this will tell u dynamic objects and if u subtract it from total object then u can get static objects

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

If your project requires this, you are probably doing it wrong.

- Guest BS.

- algos November 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create two static variables in the class. say stat_count and dyn_count
Increment the stat_count in the constructor you use to create instance of your class variables.
Increment the dyn_count in the overloaded new operator.
now static objects is equal to (stat_count - dyn_count)
and dynamic objects is equal to dyn_count.

- Praneeth December 16, 2013 | 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