HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

struct genericStr
{
	void* data;
	struct genericStr* next;
};

void generic()
{
	int i = 5;
	char c = 'c';
	float f = 5.5;
	struct genericStr g;
	g.data = &i;
	printf("data is %d\n",*((int *)(g.data)));
	g.data = &f;
	printf("data is %f\n",*((float *)(g.data)));
	g.data = &c;
	printf("data is %c\n",*((char *)(g.data)));
}

- dadakhalandhar May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent

- pirate May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dadakhalandhar:
Sorry but did you actually read the question.I was lead to believe that you need to pass an argument to insert() which can take all the the mentioned data types.

insert(void *arg)
{
//you don't know here the type of argument
//so you can typecast it to any type and this
//typecasting can lead to data abort or undesired result
//unless you know beforhand what should i typecast it to??
}
downvoting it.

- aka May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

@aka. you mean to say the following

struct genericStr g;
void insert(void* pa)
{
	g.data = pa;
}

void generic()
{
	int i = 5;
	char c = 'c';
	float f = 5.5;

	insert(&i);
	printf("data is %d\n",*((int *)(g.data)));
	insert(&f);
	printf("data is %f\n",*((float *)(g.data)));
	insert(&c);
	printf("data is %c\n",*((char *)(g.data)));
}

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

You can't do that in c untill and unless you pass a second argument.

- aka May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So the question is missing a word, "write a generic function API in C so it can ???? any data types..." Did you mean to write "write a generic function API in C so it can hold any data type..."?

I would agree though, not sure how to do that in C, but C++ provides function overloading, so maybe overload the insert() function for each data type to be inserted? That solves the problem of knowing the sizeof the data. Now the insert function can malloc the sizeof the data type and store it in there. However, when you extract the data you would still need to know the type of the data so you know how much data is held, so you might want to add that into the buffer as well.

- dn May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@dadakhalandhar: I hope you understood my point.

- aka May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aka. Sorry aka I am not getting you. Are you talking about the possibility or the consequences.

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

void insert(void *ptr, char *a) 
{
	if(strcmp(a,"char")==0) 
	{
		printf("data is %c\n",*((char *)(ptr)));
	}
	else if(strcmp(a,"int")==0) 
	{
		printf("data is %d\n",*((int *)(ptr)));
	}
	else
	{
		printf("data is %f\n",*((float *)(ptr)));
	}
}

int main()
{
	char a='c';
	int b=10;
	float c=10.2;
	
	void *ptr=NULL;
    
        ptr=&a;
        /*We can acheive the same by finding &sending the sizes of variables and using a switch loop in the insert(), But the sizes vary according to the compilers, which will make the program to function differently when compilers change.*/
	insert(ptr , "char");
	ptr=&b;
	insert(ptr , "int");
	ptr=&c;
	insert(ptr , "float");
	return 0;
}

- hem June 04, 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