NVIDIA Interview Question Software Engineer in Tests

  • nvidia-interview-questions
    0
    of 0 votes
    20
    Answers

    Give output for the following code

    #include<stdio.h>
    
    void main()
    {
        int i = 5;
        printf("%d\n",i++ + ++i);
        printf("%d\n",i++ + ++i + i++ + i++);
        printf("%d\n",++i + i++ + ++i + i++);
    }

    Please give the output with os and compiler u used with proper explanation. Getting unexpected answers..... help !!!
    This is old question posed by senior student.

    - saj1919 on June 21, 2012 in United States Report Duplicate | Flag
    NVIDIA Software Engineer in Test C

Country: United States
Interview Type: Phone Interview


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

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 on June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 ???

- saj1919 on June 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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.

- Omkar on June 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

i never understand why ppl ask such questions in interviews.
i mean what are they trying to test that they had some wasted time that they come up with weird looking expression and want others to waste there time tooo.

- guest on October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The processing follows as:

5+7=12
7+9+9+10=35
12+12+14+14=52

- pulkit on June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 ..!!!

- Gupta on June 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check your code.
My complier is giving the correct output as
12
35
52

- Ashish on August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is just a compiler problem..

- Priya Singh on September 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think v cannot use stack for this operation ...
coz v are actually modifying the memory contents
say
i=4 ,,, i++ in the sense whatsoever the case may be i will be 5

if i think in this way im getting 12 and 32 as answers
52 for the second case ... but here 50 !!! ....

- bharath on November 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

same here even i got the ans as
12
35
52

- dekontj on June 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Omkar on June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Omkar: According to your Logic, In First printf ans should be 5+6=11 . Because for postfix ++ operator, you are are not increamenting i's value in the same printf (as you have done in second and third printf)

- X on June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

please give me the solution :

we have an array of integers:
arr[] ={1,2,3,4,5}
need to write a program in C which will display all possible combinations
{1,2,3,4,5,12,13,14,15,123,124,....}

- Alok on November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please give me the solution :

we have an array of integers:
arr[] ={1,2,3,4,5}
need to write a program in C which will display all possible combinations
{1,2,3,4,5,12,13,14,15,123,124,....}

- Alok on November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

VS2010: 12;32;52 (seems it does "++i" first, then uses the result in each row; "i++" is obliviously done after each printf)

- maxus on July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

VS2010: 12;32;52 (seems it does "++i" first, then uses the result in each row; "i++" is obliviously done after each printf)

- maxus on July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

VS2010: 12;32;52 (seems it does "++i" first, then uses the result in each row; "i++" is obliviously done after each printf)

- maxmah on July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

every time it will be "i" will be changed from right most to left most..
try to print
i=5;
printf("%d%d",i++,++i);
output=6 6

- Ganeshdip Dumbare on August 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Anonymous on August 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

- dekontj on July 22, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through 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