Qualcomm Interview Question for Applications Developers


Country: India
Interview Type: Phone Interview




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

template <class T>
int sizeOf(T x = 0){
    
    return (char *)(&x + 1) - (char *)&x;
}

- n.j.joshi January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why do we want to do char * ? I would think it rather this way :

return int ((&x+1) - &x);

- iwanna February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

simply because `char` always takes one byte, the basic unit of computer word (or world) :).

- n.j.joshi February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why does this fail when x is an array?

Say

int x[]={1, 2, 3, 4, 5};

- amar November 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question: Why does this fail when x is an array? Say int x[]={1, 2, 3, 4, 5};

Answer: Because x is a pointer to the very first value in the array, which is 1. Hence the sizeof x will return the size of the pointer, which is 4 or 8 bytes depending on the system.

- George December 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We should not use sizeof operator for the array because array name treated as pointer.

- Sachendra April 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is my code

#include <stdio.h>
#define type long double
int size_of(type n)
{
	type *p = &n;
	return ((unsigned int)(p+1) - (unsigned int)p);
}
 
int main(void) {
	type x = 12;

	printf("%d", size_of(x));
}

- atinesh922 August 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
बंधुओं निम्न संगड़क प्रोग्राम में प्रिमिटिव डेटा टाइप जैसे int और char कि साइज़ प्राप्त करने का कोड विद्यमान है आगे का आप विस्तार कर सकते हैं.
ध्नयवाद
*/

#include <stdio.h>
#include <string.h>

int main()
{
int count = 0 ;
char Type[20] ;
char *DataType = Type;

printf("Enter Any Primitive Datatype: ");
gets(DataType);

if(!strcmp(DataType,"int"))
{
int a = 1;
while( a+1 != 1)
{
a = a<<1;
count++;
}
printf("Size of Int Is: %d Byte",count/8);
return(0);
}

else if(!strcmp(DataType,"char"))
{
char a = (char)1;
while((int)(a+1) != 1)
{
a = a<<1;
count++;
}
printf("Size of Char Is: %d Byte",count/8);
return(0);
}
}

- Kutte Londe December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your Program will give a good results when we ask naming the datatype.. like 'int', 'char', what about if I write something like
int x=0;
int siz= sizeof(x);

- Programming Concepts December 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the logic here?

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

Question doesn't makes sense. How does bitwise operator help in identifying 'size of operand?

to find size of any type

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

x is a variable whose size you wish to find, it wont work if you pass 'typename' instead of variable

- confused_banda December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to grow up buddy.....you should rather say the question doesn't make sense to "you"

- kutte londe December 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also:

#define MYSIZEOFT(t) ((t *)0 + 1)

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

(unsigned long)(char *)((type *)0+1) - (unsigned long)(char *)(type *)0

- Krishna December 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks kutte londe, really appreciate

- sz January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Kutte londe

Could you please explain your logic for sizeof

- Phanindra February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Kutte londe,
Just now executed your program and checked step by step .. In while loop you are shifting bit to the left wards so every time it will increment by power of two i.e., 2,4,8,16.......-2147483648(value of 2^31) when you try to left shift the bit in 2^31 value there is no space for wiriting that much big value into the byte so 'a' becomes 0 and count value becomes 32 then coming out from loop.. you are dividing count 32 by 8[8 bits = byte] = 4........

- Phanindra February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define OWN_SIZEOF(A) ((unsigned int)(&A+1) - (unsigned int)(&A))

- kondaparthi saikumar June 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Following solution has many limitation but I found only this using bit wize operator.
1. You can not return value as this is Macro with multiple line. (if you use function then you have to define type of argument)
2. You can not find size of float, double.

#define SizeOf(x) {\
    x = 1;\
    int i = 0;\
    for (i=0;x!=0;i++) {\
        x<<=1;\
    }\
    cout << i/8;\
}


int main ()
{
    int x = 1;

    cout << "sizeof int ";
    SizeOf(x) ;
    cout << endl;

    return 0;
}

- Divyang J. Mithaiwala September 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can return the value if you use like ({ ;a;}), it will return value of a

- bhallavaibhav September 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define my_sizeof(type, size)   \
{                               \
  type n = 1;                   \
  size = 0;                     \
  while(n) {                    \
    size++;                     \
    n <<= 1;                    \
  }                             \
  size /= 8;                    \
}

int main()
{
  int size = 0;

  my_sizeof(int, size);
  printf("Size of int is %d\n", size);

  return 0;
}

- Raj S October 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(in & 0xFFFFFFFF00000000) return 8;
    if(in & 0xFFFF0000) return 4;
    if(in & 0xFF00) return 2;
    return 1;

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

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

- Anonymous July 23, 2016 | 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