NVIDIA Interview Question for Software Engineer / Developers






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

Opps.. I mean
for (i = 100; i >= 0; --i)

- Khoa February 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hmm. I like Nvidia's questions. They gave out hardware/software tests at the CS career fair too. You should've seen the # of people taking tests!

Anyhow, i will not be < 0 because it's unsigned. Should be >=0. %d should work fine since it will interpret i as signed, except that may be a bug if that wasn't your original intent. Again, this may depend on the compiler's implementation as well.

- Jack February 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code.. sorry for the mistake..

unsigned int i;
for (i = 100; i >= 0; --i)
printf("%d\n",i);

- Khoa February 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

loop terminates when i = -1 which will never happen because i is unsigned, it will go back to [2exp(wordsize) - 1]

- Padmage February 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess I should've made explicit that it doesn't terminate since I already said i will not be < 0. Thanks for correcting me!

- Jack February 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will not enter the for loop because of the condition i <= 0 whereas initial value of i is 100.

- Pratik February 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think they meant to say 'i > 0' instead of 'i <= 0' in the for statement.

- Ray February 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the %d is a bug because it's not clear whether you want to print out signed or unsigned integers.

- Jack February 08, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will never execute because do to the entry conditions and the fact that i is unsigned, i can never match the loop condition (i <= 0).

- Tom March 09, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's an infinite loop (assuming the i<=0 should've been i>=0).. "i" is unsigned - so it'll always be +ve.. even though prininting it as %d will give you a signed representation, internally, it's still positive (wrapping around 0 - max_value).

- AB March 27, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey this forms an infinite loop....

consider the following code :

unsigned int i =0;
i--;

//now i = max limit of unsinged int
// for a 4 byte int i = 4294967295

- Anjani May 20, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As i is unsigned, it will never be negative, so the condition of i<=0 is a mistake, given an unsigned i, which, conceptually, cannot have a negative value. Thats the logic error in the code.

What actually happens when you compile it and run?
Depends on the system/compiler.

If an unsigned int is set to 100, that may well be positive, which means the continuation condition of the loop (i<=0) is false, meaning the loop is never entered... BUT... how is (i<=0) evaluated if i is unsigned? What is a < comparison going to do with an unsigned number? I dont' know

- Thomas S November 03, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

basically there are two bugs in this code :
unsigned int i;
for (i = 100; i >= 0; --i)
printf("%d\n",i);

1) As i is defined as insigned int so it value lies between 0 to 2^16-1 or 2^32-1 depending upon m/c.
so it will be an INFINITE LOOP.

2) The second bug is printing an unsinged value with as signed so it will be PRINTED POSITIVE AND NEGATIVE BOTH but CONDITION IS ALWAYS TRUE and hence INFINITE LOOP.

so, output will be INFINITE LOOP with printing value of i oscillate between 0,POSITIVE and NEGATIVE during the execution of the loop.

- guest May 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

were can i find NVIDIA questions related to Electronics topics?!

- shiva September 20, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code.. sorry for mistake

unsigned i;
for(i=100;i>=0;--i)
printf("%d/n",i);

- Munivelu K Reddy October 20, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hmmm.. its an infinite loop n the bug in the code is that i should be >0.

- sanju October 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is an infinite loop.
Here is why....

from 100 upto 1 it will work normally ...
after it prints 1 it will check is i>=0 yes! so it will do (--i)
then it will print 0 will check is i>=0 yes! so it will do --i, but since
i is unsigned i-1 will make i as 0xFFFFFFFF . So for next time in loop it will print 0xFFFFFFFF (or equivalent in decimal) and continue since (i>=0).

Solution is make i>0

- Gaurav April 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is only one correction required to be done in this code to run the code properly. and that is you have to write the code in following way :

unsigned int i;
for (i = 100; i > 0; --i)
printf("%d\n",i);

this will solve all the problem.But be careful in not to use "%d" if you want ti print the unsigned int values."%u" in place of "%d" will make your life easier.

- critical section May 23, 2008 | 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