Amazon Interview Question for Software Engineer in Tests


Team: Kindle
Country: United States
Interview Type: Phone Interview




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

In java, the same programe will give us the output , suppose if our input is n=10 means then the output will be 10 10 10 10 10 10 10 ... at last JVM will through the StackOverFlowException

- Beginner March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right... because the value is of n is post-decremented, so n=10 is passed every time.

- silvermist March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I totally agree . I will generate the same out put again and again and ultimately give a stack overflow in Objective C as well. But the code runs perfect when its pre-order incremented .

- yasha.khandelwal02 April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 5 vote

It's an infinite loop printing the same number that you fed the original call to MyFunction. Does not matter if the number is positive or negative. The postfix decrement returns the original n during evaluation, so you'll be stuck calling MyFunction(n) with the same value of n. And you'll never get to the second print due to the infinite loop, so the same number will be printed over and over and over again.

- just_passing_through March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are right... since its myFunction(n--) so it will always pass n to next function call

- vik April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If n = 10 it will print 10 until it runs out of memory and produce a stack overflow error (infinite loop). The second println(n) is never used because of the post-decrement of (n) in the recursive method call. If it were --n then the result would be 10,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9 which is n-1. 10 to 1 for the 1st print(n) and 0 to 9 for the second print(n). Recursive method calls makes copies the code saves them to memory. After the (if) statement returns true the copies are returned in reverse order, LIFO.

- Leon Bland March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

nice question ...

- amarkant March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

infinite loop and stack overflow

- vikki March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

infinite loop and stack overflow

- vikki March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

it shows an decreasing order and an increasing order of numbers.

- krishna May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

negative integers infinite loop

- Vishi March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it won't print 0 ... becoz if (n==0) return.... then it will print 1 again for the 2nd print

- Anonymous March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

for non negative no. it show series of no as n n-1 ... 1 0 1 2 ... n-1... :)
for negative no. its will be an infinite loop...

- aryanmain March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why it is infinite loop? Sequence is like below...
-int_min...... -1 0 1 2 ...... +int_max

if you pass -ve number then it will reach -int_min and then it go to +int_max and then deceased and reach to 0 and then function will return

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

Thanks,I forgot to mention that n is positive

- Ray Sun March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it will print as n n-1......2.1.1.2.....n-1.n but no 0
as d code says.
"if (n == 0)
return; "

- jim March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it will print 0 for the second printf(n) statement for n = 1

- aryanmain March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

negative number makes it run in an infinite loop....and finally segmentation fault in gcc

- Anonymous April 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

doesn't matter its java or c++. it will be infinite loop and will throw an stack overflow exception.
Also one thing if its --n instead of n--, what do you think the output will be.

- naphstor March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if n==10
then it will be 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9

Excellent question!

- sreemana@buffalo.edu March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you share your logic?

- just_passing_through March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

n-- will be infinite loop
--n will be 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9

- Ray Sun March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if its --n the output will be 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9.
The reason is when first time (assuming n=5) myFunction(5) is called which stores 5 into the program stack. next call is myFunction(--n) i.e. myFunction(4) is called which creates value 4 in the stack. similarly it stores till 0. now when n == 0, the myFunction(0) returns and the next statement i.e. print(n) after the myFunction() in else gets executed. print(n) reads the last value in stack which is 1 and prints it, then this myFunction() returns and so print(n) gets executed which reads next value in stack i.e. 2. Similarly it does it for each value till 4. But when it prints 4, it reaches to the stack position containing element 5 but at that moment there wasn't any recursive call made and so it returns to main function without printing 5 in the last. So that's why it printed the above output.
Hope everyone got it what i meant to say. Everything is because of recursion.

- naphstor March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Infinite loop

- nandkarthik March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stack Overflow because n-- will send value n first then subtract it so 10 will be passed over and over

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

it is a odd palindrome sequence with middle element as zero.

- krishna May 22, 2012 | 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