Microsoft Interview Question for Software Engineer / Developers






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

#include<stdio.h>
#include<stdlib.h>

struct PARENT_vtable { 
	int (*Init)(); 
	int (*Add)();
}; 

struct CHILD_vtable { 
	int (*Init)();//child will override 
	int (*Add)();//parent will define
	int (*Sub)();//child will define
}; 


int parent_doInit() 
{
	printf("parent init\n");
}

int child_doInit() 
{
	printf("child init\n");
}

int parent_doAdd() 
{
	printf("parent add\n");
}


int child_doSub() 
{
	printf("child sub\n");
}

struct PARENT_vtable PARENT_vtbl = {&parent_doInit ,&parent_doAdd }; 

struct CHILD_vtable CHILD_vtbl = {&child_doInit, &parent_doAdd ,&child_doSub}; 


struct PARENT_data 
{ 
	int x; 
}; 

struct CHILD_data { 
	int a;
}; 


struct PARENT 
{ 
	PARENT_vtable * vptr; 
	struct PARENT_data parent; 
}; 

struct CHILD { 
	CHILD_vtable * vptr; 
	struct PARENT_data parent; 
	struct CHILD_data child; 
}; 

struct PARENT * CreateNewInstance() 
{ 

	struct PARENT * Cthis = (struct PARENT*)malloc(sizeof(CHILD)); 

	Cthis->vptr =(PARENT_vtable*) &CHILD_vtbl; 

	return Cthis; 
} 


main() 
{ 

	struct PARENT * Kids1=CreateNewInstance(); 

	((CHILD_vtable*)Kids1->vptr)->Init();
	((CHILD_vtable*)Kids1->vptr)->Add();
	((CHILD_vtable*)Kids1->vptr)->Sub();

}

- Hemant Jain February 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is all this...i dont understand a word

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

just use the key word struct instead of class.

struct and class are exactly similar (they both can have constructors). But struct's members are public by default and class members are private by default.

so declare a struct and have its constructor in its private region.

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

You need to read the question again!

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

I am bit confused!!
"Using struct in C"
We have no concept public/private member in C struct.
Moreover, member functions are undefined in C struct.
I might understand the question wrongly so let me clear

- Shwetank March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can somebody clarify this question? In C, struct is public, there are no access specifiers in C because data abstraction is an OO concept. So does this imply that we need to mimic OO behavior using struct in C?
@hyan, what did the interviewer say?
@hemant - can you explian your solution?

- M March 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is so confusing, someone please clarify this...

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

This is not confusing. C struct does not allow c++ class features. However, C allows function pointer. Therefore, the point is how to use function pointer to implement c++ constructor concept.

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

As you can see from the responses more than one person found the question confusing, including myself. I believe it is primarily because of the part of your question that says, ‘using the struct in c?’. This is confusing because c++ has the equivalent of the struct in c. So does the question mean, "How do you do this in c++?" or “How do you do this in c?"

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

i saw Hemant Jain's code somewhere before. But I am not very convinced by that. Somebody else has better idea?

- hyan2008 April 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, see this link. It is helpful
http://www.keyongtech.com/4831974-how-to-implement-c-class

- Sharath September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sounds perfect Hyan. for more of C++ flavor into this, we could also add this pointer into it :).
polymorphic C++ classes includes vptr and vtable that you have already done nicely.
If we add this pointer to all member functions that would add more of C++ flavor


int parent_doAdd(struct PARENT *this)
{
printf("parent add\n");
}



int child_doSub(struct CHILD *this)
{
printf("child sub\n");
}

- Arif.Ali.Syed July 11, 2010 | 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