Samsung Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

@ varun
i think it is wrong it will always give 1.
it should be
#define sizeof(data)
{data *p=0; size = (char)(p+1) - (char)p }

- getjar.com/todotasklist my android app November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, apologies, I missed the char part. I had it there when answering.Thnx for pointing guys.

- Varun November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ eugene.yarovoi
that is the side effects of macros we must take care of side effects of macros
any way it is correct

- getjar.com/todotasklist my android app November 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We Should keep in mind...Sizeof() operator is implemented at Compile time not at run time...So I don think the solution like
/*
#define sizeof(data)
{data *p=0; size = (char*)(p+1) - (char*)p }

*/

It is not a good approach..It must a Parser approach...

- User November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

either int or char*.

(char*)(&data+1)-(char*)(&data)

- Charles December 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@techcoderonwork
Why is it required to cast to char.

- Anonymous November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

it should be char* sorry for mistake

@anonymous char* is necessary
becoz see here

int *p; // suppose p points to 100th memory location

when we do something like
(p+1)-(p)//(104-100)
but this expression will not give 4 but it give 1
becoz p is a pointer to an integer and such expressions tells how many elements are there in between (p+1) and p

when we typecast
char*(p+1)-(char*)p pointer p is converted in char*
(104)-(100) which is equals to 4 which is the size of int

hope that it is now clear to you dear.

- getjar.com/todotasklist my android app November 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey
u said we have to typecast first ,but why not typecast to (void*) or (int*) why only to (char *)

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

hey
u said we have to typecast first ,but why not typecast to (void*) or (int*) why only to (char *)

- deepak February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Varun,
I'm not sure on the below solution
For variable, we can have something like this.

int i ;
sizeof(i);

#define sizeof(data)
{void* p=&a ; size = abs((p+1) - )p) }

not sure will it work or not.

- Anonymous November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Sorry, the above solution will not work.

I hope we can use templates here

template<class unknowntype>

sizeof(unknowntype variable) // int i ; sizeof(i) ;
{
unknowntype* ptr = 0 ;
size = abs((ptr + 1)-ptr )
}

By theory this must work. :-)

- Vijay November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wow.. this sounds correct. But, need to cast ptr to (char *)

template<class unknowntype>
int sizeofType(unknowntype variable) // int i ; sizeof(i) ;
{
int size = 0;
unknowntype* ptr = 0 ;
size = abs((char *)(ptr + 1)-(char *)ptr );
return size;
}

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

But, How this will work in C?

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

Guys, can someone tell me why casting to char* is required.

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

the reason is explained above

- getjar.com/todotasklist my android app November 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about this one, works for types and variables:

#define SIZEOF_type(T) \
        ((char *)((typeof(T) *)0 + 1))

- pavel.em November 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One Doubt:( On GCC compiler if i do sizeof('a') it gives 4 not 1..Why???

If this is a behavior Then How will we make our Code generalized.

We need to parse this Argument..In My Opinion sizeof() is not that straight forward

stuff as People Think.Please help

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

"One Doubt:( On GCC compiler if i do sizeof('a') it gives 4 not 1..Why???"

printf("Size of %d\n", sizeof(char));
    printf("Size of %d\n", sizeof('a'));
    printf("Size of %d\n", sizeof((char)'a'));

The first sizeof is applied to a type - it gives the size of it correctly (1)
The second is applied to an integer expression, 'a' is converted to int whereever it appears in the code, so the size of int is returned (usually 4)
The third one, 'a' is converted to int and then to char, so sizeof will return 1.

- Selmeczy, Péter December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Working code

#include<stdio.h>
#define size_of(data) data *p=0; size=abs((char*)(p+1)-(char*)p)

int main()
{
int size;
size_of(double);
//sizeof(float);
printf("%d\n%ld",size,sizeof(float));
return 0;
}

- Abhishek February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why we need to give abs()

- saurabh February 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

#define My_Sizeof ( type ) ( char * ) (&type+1) - ( char* ) ( &type )
int main()
{
	int a;
	printf ("%d", My_Sizeof (a) );
	getchar ();
	return 0;
}

- nueman fernandez May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(x) ((char *)(&x + 1) - (char *)&x)

- Anonymous March 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(_x_) ((unsigned int)(((typeof(_x_) *)0)+1))

- Toseef Koliyariwala January 29, 2015 | 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