Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
6
of 8 vote

Assuming size of char is one byte this can be implemented as following macro.

#define mysizeof(data) (char *)(&data+1)-(char *)(&data)


U can also make it function , but C doesnt support fn overloading

- techieDeep January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution.. Can you explain how this is working for an array? I thought for int a[]={4,5,6}; would give me 4 instead of 12. but it does correctly give me 12;

- isandesh7 January 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Each int is 4 bytes, he is giving number of bytes difference between data and data + 1

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

This implementation will not work for -
mysizeof(int).

Is there any way to achieve it ?

- Neha February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain why are you casting yo char* ? I know it works perfectly, but not getting the reason for typecast.

- Jack February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@neha
we can implement sizeof( type ) something like this.
#define sizeof_type( type, var ) { type temp; var = mysizeof( var ); }

but you will have to call it in a non conventional way.

size_t bytes;
sizeof_type( int, bytes);
sizeof_type( int *, bytes ); etc....

- Arun February 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@jack, typecasting is done to convert to char pointer because if it is ,lets say,a float pointer,the statement

(&data+1)-&data,

would give the correct size/4 (4 i.e the size of float)

whereas

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

gives us the correct size/1 (1 i.e. the size of char)

- akie July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{ #define my_sizeof(type) ({typeof(type) _a ; (char *)(&_a+1) -(char*)(&_a);}) }
This will work for following cases
my_sizeof(int)
my_sizeof(float)
my_sizeof(char)

- raghumag September 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In case of C++ this can be implemented with a template function, and it can work for object and type as well..

- Anonymous July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

template<class Type>
int mysizeof( const Type &)
{
Type arr[2];
return (long int)&arr[1]-(long int)&arr[0];

}

//pass in variable of required datatype

- Anonymous October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Any Idea how to implement in JAVA

- Anonymous January 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Good solution.. Can you explain how this is working for an array? I thought for int a[]={4,5,6}; would give me 4 instead of 12. but it does correctly give me 12;

suppose memory location for a[] starts with 2000, then a[]+1 will point to 2012 and not 2004.
This is because a[] is referring to the complete array and hence a[]+1 will refer to a memory location just next to the last element of the array.
So, the output with mysizeof(a) gives you 12 and not 4.

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

one complicate way to implement this is to use RTTI, using typeid and calculate the size of certain data.

- SuperSoulHunter February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why do we have to cast it with (char *) can anybody please explain?

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

Hi,
I think they have stated the reason in the answer itself. When we talk about sizeof() operator, we are interested in getting the size of the data type/ variable in terms of no. of "BYTES". And since a char type would typically be a BYTE long, they casted it to char* (so as to measure the datalength in bytes).

- aago March 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is the code for getting the size of structure??
for eg:
struct node{
char c;
int i;
};

- amrit.baxla June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(d) ({ \$
typeof(d) _a; \$
(size_t) ((char *)(&_a + 1)-(char*)(&_a)); })$

This will work for all standard & user data types.

- Anonymous December 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(d) ({            \$
          typeof(d) _a;            \$
         (size_t) ((char *)(&_a + 1)-(char*)(&_a)); })$

This will work for all standard & user defined data types

- Anonymous December 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(d) ({            \$
          typeof(d) _a;            \$
         (size_t) ((char *)(&_a + 1)-(char*)(&_a)); })$

This will work for all data types

- raghumag December 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Someone has asked sizeof(type). I have the answer but with a little bit modification to signature. - sizeof(type, size) - here size if the output parameter name. Refer this example-

#include<iostream>
#include<string>

using namespace std;

#define mysize(x,y) { \
x *p; \
y = (char*)(p+1) - (char*)p; \
}


int main()
{
int size = 0;
mysize(int, size);
cout << "size = " << size << endl;
}

- Anonymous December 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A trick to get sizeof(type) but with a modified signature -

#include<iostream>
#include<string>

using namespace std;

#define mysize(x,y) { \
        x *p; \
        y = (char*)(p+1) - (char*)p; \
}


int main()
{
  int size = 0;
  mysize(int, size);
  cout << "size = " << size << endl;
}

- Anonymous December 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is an implementation.


#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
int main()
{
double x;
printf("%d", my_sizeof(x));
getchar();
return 0;
}
You can also implement using function instead of macro, but function implementation cannot be done in C as C doesn’t support function overloading and sizeof() is supposed to receive parameters of all data types.

Note that above implementation assumes that size of character is one byte.

Time Complexity: O(1)
Space Complexity: O(1)

- code_jerk January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work for types though.
For example a function that takes an array of int and the array len:
myFunc(data, mysizeof(data) / mysizeof(int));

You should also add '(' and ')' around the expanded macro because of the '-' in the middle, to allow for its use in calculations like:
size_t foo = mysizeof(var1) / mysizeof(var2);

So:
#define mysizeof(data) ((char *)(&data+1)-(char *)(&data))

- Sie March 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

#define MYSIZEOF(X) ((X*)0 +1)
int main()
{
printf ("%d", MYSIZEOF(int));
return 0;
}

- eric wu January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-2
of 2 votes

YEAH!

- Anonymous January 29, 2013 | 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