Amazon Interview Question for Testing / Quality Assurances






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

can someone explain clearly how come the answer is 20, i'm not able to follow EZ's response. help me out plese

- Anonymous September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The first critical piece is that Fork() returns an integer - it returns 0 to the child process (created by the fork) and a positive number (the process ID number of the created child process) to the parent. The second critical piece is that && and || are evaluated lazily in C - that is, the program only evaluates the second piece if the result is still ambiguous. (ie 'true || foo()' always evaluates true, so why run foo()?) The third critical piece is that && is evaluated before || in C.

In the first line, the original process (call it P0) forks and makes no use of the returned value, so we have 2 essentially identical processes entering line 2 (now P0 and P1).

In the second line, each process runs the first Fork() and spawns a child (P2 and P3). P2 and P3 both return 0 from that fork, while P0 and P1 return a positive integer. For P2 and P3, that 0 is evaluated as false, and so they don't bother to run the second fork. P0 and P1 do run it, creating P4 and P5. At this point, P0 and P1 have returned true to the first and second forks of line 2, and so have evaluated the && as true - no need to evaluate the other side of the ||, so they skip to line 3. P2-5 have each returned false to one of the forks, so they return false to the && and proceed to evaluate the final Fork() of line 2, creating processes P6-9.

On line 3, every process (and there are 10 so far) runs the fork, for a total of 20 processes.

- Das Brose September 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I finally understood how it works. Thank you Das Brose !

- Boesemann February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great work Das. Thanks a lot for the help..

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

Very clear explanation Das. Thanks

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

Great work das.. very clearly explained

- Priya October 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great work Das

- vegeta March 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great explanation .Thank you

- Nans September 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks a lot das brose... very good explanation

- ashjwr November 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thank u sooo much Das Brose , i was trying to solve it for sooo long. i tried every where but couldn't understand but finally understood because of your detalied and lucid explanation
hats off to u man!!
thanks

- Anonymous February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

I got 20
basically I got 10 processes for the parent after the first fork.
That will be similar for the child after first fork.
so total 20 including the original one.
I guess 15 cannot be answer for sure as it will be some even number

- desi grad September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

19 new processes will be created. 20 including the original one.

- DeathEater September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

fork return 0 for child process, and non-zero for parent.
L1: fork
L2: fork && fork || fork
L3: fork.

P0, after L1, got P0, P1. Say P0 is parent, the origin
handle P0 first
P0, L2, after first fork, got P0, P2. P2's L2 work done.
P0, L2, after 2nd fork, got P0, P3. P0's L2 work done.
P3, L2, after 3rd fork, got P3, P4.
For P0, P1, P2, P3, P4, got P5, P6, P7, P8, and P9 respectively after L3 fork.
Similar for P1 from L2.
Total got 20 processes, minus P0, create 19.

- EZ September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Answer is - 20
# of process type - 1 = 10
# of process type - 2 = 8

+ one parent and one grand-child from parent.

- saumya.wipro April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the fork() in OR part will get executed only when the first part gives FALSE, right? And we don't know the value returned by the two forks in this part, it may return 0 or 1. so depending on the return value it'll be decided that the second/ORed fork() will get executed or not. In short you can't predict the process count as asked in the question.

- abe dhakkan September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i also get 20

- ld September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ans is 20 i wrote a program for it :)

- cunomad September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you show your program? Did you use ps to check the processes?

- ld September 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if there are n forks in series, then overall, 2 power n processes will be created, i.e one less than 2 power n
New process will be created,


So If we take the above process as a series of processes.
there are 4 fork statements in series, so there exist at most 16 procesess.

please correct me, if i am wrong.

- sonia October 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if there are n forks in series, then overall, 2 power n processes will be created, i.e one less than 2 power n
New process will be created,


So If we take the above process as a series of processes.
there are 4 fork statements in series, so there exist at most 16 procesess.

please correct me, if i am wrong.

- sonia October 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sonia.u r ri8.i dnt knw hw d guys aboce r arriving at these fanciful numbers.juz draw d tree man.15 new child processes are created + the original mother process.

- Anonymous October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks Das Brose!

- Anonymous October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Total processes are 20 including parent process (i.e., main process)

- S ... November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Brose,you did a wonderful job! Thank you for your explanation.

- xuanxuan November 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually it depends on whether the compiler implements short-circuit evaluation. If it does, then the second statement will evaluate as follows

fork
/\
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
&& fork && fork
/\ /\
/ \ / \
/ \ / \
/ \ / \
/ \ / \
&& fork && fork || fork || fork
/ /\
/ / \
/ / \
/ / \
/ / \
SC || fork || fork


where SC is the result of short-circuit evaluation. So in the presence of short-circuit evaluation the answer is 20 threads.

- Anonymous May 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

&& implies "short-circuit evaluation", i think it is dependent on the operator. & definitely does not have this.

- Anonymous May 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OMG!
& is a bitwise AND operator!
&& is a logical AND!
Of course "& does not have _this_" because it calculates an integer, not a boolean value!
The behavior of && totally depends on the compiler.

- Alexander Amelkin February 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

fork return 0 for child process, and non-zero for parent.
L1: fork
L2: fork && fork || fork
L3: fork.

P0, after L1, got P0, P1. Say P0 is parent, the origin
handle P0 first
P0, L2, after first fork, got P0, P2. P2's L2 work done.
P0, L2, after 2nd fork, got P0, P3. P0's L2 work done.
P3, L2, after 3rd fork, got P3, P4.
For P0, P1, P2, P3, P4, got P5, P6, P7, P8, and P9 respectively after L3 fork.
Similar for P1 from L2.
Total got 20 processes, minus P0, create 19.



i have a doubt at l2 whether compiler interprets l2 as (fork() && fork()) || fork()
or in some other way??
if it interpret it as (fork() && fork()) || fork() then p2 job is not done with the first fork it self it will execute the fork after || so it will create one extra process and that extra process will create one more extra process on line 3 so total no of process is 21.correct me if i am wrong.

- sravan August 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Po                            
    Po                            ,      P1
  Po,          P2 ,              P1,       P3
Po ,  p4  , p2,      p5   ,  p1,    p6,    p3 ,   p7
Po,P8,P4,P9,P2 ,P10 ,P5, P11,P1,P12,P6,P13,P3,P14,P7 , P15

Total = 15

- Nirmal July 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks a lot Das for your precise explanation

- Raveena September 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main()
{
if(fork()||(fork()&&!fork()))
printf("AA\n");
else if(!fork())
printf("BB\n");
else
printf("CC\n");
}

I tried solving this, got AA 2 times, BB 2 times and CC 2 times but not 100% sure
can someone try solving this and see what could be the output... thanks in advance

- RCB September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain how you got the ans (AA 2 times,BB 2 times,CC 2 times)for the above prog ? Thanks in advance

- Das September 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

20 and the answer is 19 process created.

- Elavarasan October 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

20 and the answer is 19 process created.

- Elavarasan_psg October 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fork();
fork();
fork();
fork();
creates how many child processes

- Anonymous August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

16

- samie: September 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

15 new child processes

- Nitin October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

15

- Nitin October 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Fork spawns new threads, not processes. So the answer is 0.

- anon September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong. fork creates new processes.

- shiv October 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@anon:
I guess the intent is clear enough. the interviewer is expecting some fancy numbers

- klpd September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I got 27 but not 100% sure about it. for anon, go back to ur OS class again. For abe, fork() returns 0 if it's the child process.

- Dr nerd September 17, 2009 | 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