NVIDIA Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Phone Interview
U seem to be correct but i tried same code with same compiler ( cfree ) on xp and win7 .... and i am getting different answers. Is it also machine/os dependent ???
@Guru
Thank you for pointing it out to me. I forgot to take compiler and machine dependency into account which reminds me of a similar question based on determining the size of the object of a class.
There cannot be one predictable output of the above code.
i got answer as
12
35
52
but
When i ran this program on machine i shocked to get the answer 12,32,50.
i am writing here what concept i used.
If we do some operation in printf statement on same element then we have to use stack.
so in first printf statement:-
structure of stack is
++i (top of stack) => 6
i++ => 6
and after the execution of this value of i is 7 and i++ + ++i is 6+6 = 12
in second printf statement
structure of stack is
i++ <- (top of stack) = > 7
i++ =>8
++i => 10
i++ (lower index of stack) => 10
and after the execution of this value of i is 11 and i++ + ++i + i++ + i++is 10 + 10 + 8 + 7 = 35
in third printf statement
structure of stack is
i++ <- (top of stack) = > 11
++i =>13
i++ => 13
++i (lower index of stack) => 15
and after the execution of this value of i is 15 and ++i + i++ + ++i + i++ is 15 + 13 + 13 + 11 = 52
but When i ran this program on machine i shocked to get the answer 12,32,50.
i dont know what concept they used ..most probably side effect..
let me know if some have answer of this question with proper explanation.
thanks in advance ..!!!
When I compile the code on gcc compiler and run it in a Unix environment I am getting the answer as 12,32 and 50.
One way to break down the problem is as follows:
First printf: During this stage the ++i causes i to become 6 so the addition is 6+6 = 12 and then i++ is executed causing i to be 7.
Second printf: Here again the ++i causes i to be 8 and so the addition is 8+8+8+8 = 32 and then i++ is executed three times causing i to be 11
Third printf: This is somewhat tricky part. Here the first ++i causes i to be 12 and hence i remains 12 till the second ++i which causes it to become 13 so in conclusion the addition becomes: 12 + 12 + 13 + 13 = 50 and now after this statement we have the two i++ which cause i to become 15 in the end.
This is the only explanation i can think of. Also you can verify this by inserting alternate printf statements in your existing code to print values of i at different stages.
Answer is undefined behavior. This is because the '+' between i++ and ++i is NOT a sequence point. A sequence point is a point in code where side effects like ++ of all previous computations is completed. Example of sequence points are ; (end of statement), ? in ?: operator, logical operators like &&, ||, etc.
when i first solved the problem i got 12,35,52 only .
but ans is 12 ,32,50
here is the explanation :
1)i++ + ++i = 6+6 =12, due to post increment value .
after this step value of i=7.
2) i++ + ++i + i++ + i++ = 8 + 8 + 8 + 8..
value of i will become 8 when it reaches ++i, so the effect of 1st two i's will be 8+8.
but due to post increment , the value of next i++ will be 8 not 9.
value of i after this step =12.
3) ++i + i++ + ++i + i++= 12 + 12 + 13 + 13 =50
similar explanation for this also.
The result of above code is undefined and may be different in different compilers.Because as per the c++ standers if we modify a variable more then one time in a single statement the result is undefined and based on compiler implementation.
- Guru June 22, 2012