Interview Question


Country: United States




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

Do you think question 4 and 5 should be?

4. int a[5];
return (&a[5]<&a[4]);

5. int a[5];
return (&a[5]<&a[6]);

- Anonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think 4, 5 and 6

- arpitg July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

5 and 6.

4 is well defined in C.

- Taemin July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In what way is 4 well-defined?

- Anonymous July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int a[5];
a+4; /* Good */
a+5; /* Good */
a+6; /* Bad. Undefined */
a[5]; /* Bad. Undefined*/
a[6]; /* Also Undefined */

Right?

&a[5] is equal to &*(a+5).
if '&' is followed by '*', C compiler remove them.
So &*(a+5) is equal to a+5.
It's valid.
It never deference 5th element.

- Taemin July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It never deference 5th element -> It never dereference 5th element

- Taemin July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why would a + 6 be undefined?

- eugene.yarovoi July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 is also undefined. There are two expressions here for =: a[i] and i++. Their order of execution is unknown as sequence point is at semi-colon.

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

Surprise, surprise, but it's 3 and 6.

Shifting something left by more than or equal to its bitwidth is undefined. An int can only be shifted 31 or less. That's assuming an int is 32 bits on your architecture, of course.

4 and 5 are actually perfectly fine because an expression like &a[i] gets translated to (a+i) without ever dereferencing that address. So we're just comparing pointers and never dereferencing them.

- eugene.yarovoi July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

options are
(a).1,2,5
(b).1,5,6
(c).1,2,3,5
(d).All of these

So , what will you tick ?

- Shobhit July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, 1 is also undefined. The others...I don't see how. What's their definition of undefined? I mean, a+5 could be less than a+4 if it overflows, and that's situation and architecture dependent, but I wouldn't say that's undefined.

- eugene.yarovoi July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 is undefined . but how ??
e.g. int a[5]={0};
int i=2;
a[i]=i++;
for(i=0;i<5;i++)
printf("%d\n",a[i]);


imp thing is a[2]=2 .

- Shobhit July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

6 is not undefined.

comma operator is a sequence point and that means the order of the operations inside the (++i,i++,i) is well defined. That is, the above expression can be translated to something like this, which is well defined:
++i; i++; i = i;

see this answer (it contains examples similar to 1 and 6):

stackoverflow.com/a/4176333

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

6 is not undefined.

comma operator is a sequence point and that means the order of the operations inside the (++i,i++,i) is well defined. That is, the above expression can be translated to something like this, which is well defined:
++i; i++; i = i;

see this answer (it contains examples similar to 1 and 6):

stackoverflow.com/a/4176333

- dev.cpu July 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well 1,2,3,5 will show undefined behavior because of following reasons -
1. The unary operator ++ is always very ambiguous and it's precedence changes from machine to machine, so at a particular machine you just can't predict whether the a[i] will get the new or old value of i, thus this is undefined.

2. It will be an integer overflow. As you cannot increase the value of INT_MAX and try to print it with integer specifier, as it is the maximum value of int.

3. Here the width of int is 32 and you are shifting 1 left by >= width which will cause undefined behavior.

5. It is very obvious that you cannot use &arr[6] as it will be an address of just a random chunk of memory and it will always give an undefined result.

- Spock July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

&arr[6] is (arr+6). It involves no accessing of what's at that location.

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

Also, integer overflows aren't undefined, last time I checked. Integers behave in a specific and not undefined way when they overflow.

- Anonymous July 07, 2012 | 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