Amazon Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
2
of 4 vote

20

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

This is how I got 20

A: fork();
B:fork() && fork() || fork()
C:fork();

A: fork() 2 processes (1 parent and 1 child)
B: fork() && fork() || fork() 4(both fork) + 4(both fork) + 2(only child do this fork) = 10 since this is a single execution statement we need add them
C: fork() 20

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

Why B forks 4 processes?

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

When a process execute fork() then we have two processes in total. If two processes fork() then we have 4 in total. Before B execution starts there are two processes.

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

Your answer is correct but the calculation is buggy. I have shown below a proper calculation.

- Russell March 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

I think it's 20 including the program's original process. Here's a few things that might help when solving it:

- Labeling the fork calls and drawing a process spawn tree helps a lot
- fork() returns zero in the child and returns the child's PID in the parent. This has an effect on which of the three middle fork() calls get executed in each process.
- logical AND has precedence over logical OR

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

sounds correct. In the second line only first two folk() will work.

- BMLee March 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

20

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

its 15

- mani March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

First of all , please explain ur answer how did u get it?
my answer is 12
A: fork();
B:fork() && fork() || fork()
C:fork();
From A two process are there Parent and child
now both execute from B
so for both total process will be same
we need not count for both but one
lets count for parent
after statement B; parent executes first fork of and operation and get a non zero value
so now two process in the system
p1 and c1
p1 executes second fork of and operation and gets non zero value and c2
so no need to execute OR operation
so due to p1 ,c1 and c2 gets spawned
now both c1 and c2 should execute from next statement i suppose
for each 1 one process (p1,c1 , c2) fork() of C gets one more process
so total = 2 *[ (p1 + 1)+(c1+1) + (c2+1)] = 2*(6) = 12
any Comments ??/

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

f#x : fork no. "x" as appearing in program text
0 : return value of child process of the fork that created the branching
1 : return value of parent process of the fork that created the branching

                                                  control branches
                                                  /                          \
                                                 /                            \  f#1
                                                0                             1   this subtree has equal #leaves as its sister
                                          /                \ 
                                         /                   \  f#2
                                        0                     1
                                      /    \                    /  \
                             f#4  /       \                /       \  f#3
                                   0         1            0         1                                             
                                 /\            /\           /\            |\
                        f#5  /   \   f#5 /  \         /  \ f#4    |  \  f#5
                              0   1       0  1       0   1       0   1   
                                                         /\     /\
                                                 f#5 /  \    /  \  f#5
                                                       0  1 0  1
                                         
total no. of leaves = 2* (10) = 20

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

Please copy the whole of above comment and paste as "Times New Roman" in any editor (I have checked MS word) with size 10 to see it properly.

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

24.
I agree with the anonymous guys explanation

@ Russel: in the 'fork() && fork() || fork()' expression, since '&&' takes precedence over '||' first the 2 processes from (a) will each produce 4 processes each giving a total of 8 processes (4 parents and 4 children).... now the '|| fork()' process will operate on either of the two options:
a) the child process from the first fork()
b) the child processes (4 of them) from the 'fork() && fork()' operation.

Which one is correct? I believe its the latter.
So the '|| fork()' operates on the 4 children giving a total of 4+8 = 12 processes.
This when passed through the final fork, gives out 24.

Please correct me if Im wrong here.

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

Will it be 12? I got 12 in the following manner:
Let's start with a process, say A
Process A
1. Fork()
A(PID =1) and B(PID=0)

2. Fork()&&fork()||fork()
IN the fork() && fork() || fork()
A executes 1st and 2nd forks. It doesn’t execute the 3rd because 1&&1 (since fork() returns 1 for parent process and 0 for child process) results in 1 and hence the 2nd statement of || is not executed. So now we have 2 more child processes of A.

B also executes only two fork() processes as its PID=0 the && will not check the second condition as the 1st fork return 0. Hence it creates 2 more child processes

3. Fork()
So all the 3+3=6 processes execute the 3rd line which results in 12 processes.

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

{
{
{}
}
}

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

Here the difficult part is to figure out how many processes fork() && fork() || fork(); created

A: fork();
// at this stage above fork() created 2 processes

B: fork() && fork() || fork();
p --- 1st fork()
/ \
p c --- fork() 0 for child, 0 && fork() || fork(), child fork() after && and go straight to fork() behind || which created 2 processes
/ \ / \
| c | | --- for parent fork() in 1 && fork() returns 1, it will skip fork() after ||, no process is created for parent.
| / \ | | --- for child fork() in 1&& fork() returns 0, it goes to fork() after || which created 2 processes.
| | | | | --- now we have 5 processes

C: fork();
// above fork() created 2 processes.

Total number of processes becomes 2 x 5 x 2 = 20.

- groomnestle December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here the difficult part is to figure out how many processes fork() && fork() || fork(); created

A: fork();
// at this stage above fork() created 2 processes

B: fork() && fork() || fork();
p --- 1st fork()
/ \
p c --- fork() 0 for child, 0 && fork() || fork(), child fork() after && and go straight to fork() behind || which created 2 processes
/ \ / \
| c | | --- for parent fork() in 1 && fork() returns 1, it will skip fork() after ||, no process is created for parent.
| / \ | | --- for child fork() in 1&& fork() returns 0, it goes to fork() after || which created 2 processes.
| | | | | --- now we have 5 processes

C: fork();
// above fork() created 2 processes.

Total number of processes becomes 2 x 5 x 2 = 20.

- Gallon December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume A=B=C=D=E=fork();
Question is
A(); // Will be called twice means (A()=0 and A()>0). Iterations 2
B()&&C()||D(); // When B() = 0 , D() will be called means (D()=0 and D()>0). Iterations 2
// When B() >0 and
// When C()=0 , D() will be called means (D()=0 and D()>0). Iterations 2
// When C()>0 , C() will get called means (C()>0). Iterations 1
E(); // E() will be called twice means (A()=0 and A()>0). Iterations 2

Total Iterations = Iterations_of(A()) * Iterations_of( B()&&C()||D() ) * Iterations_of(E())
= 2 * (2+2+1) * 2
= 20.

Total processes to be spawned = 20.

- Mayur Khupat August 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is 20.
Initially there is only one parent process P.
The first fork() results in two processes overall namely, P (parent) and C (child). P has return value of some positive number which is the pid of child while child has return number 0 because of its successful creation.

Now the first fork() on the second line will execute. This will result in two more child processes C1 and C2 spawning from two parent processes P and C. P and C will have return +ve return values while C1 and C2 will have return value 0.

Now the '&& fork()', ie the second fork on the second line will only execute if the first fork of this line returned a +ve integer. So, only the processes P and C will execute this '&& fork()' statement and result in one child process each, C3 and C4. Now, P and C will have +ve values while C1, C2, C3, and C4 will have 0 value. The '|| fork()' is only executed if 'fork() && fork()' returned 0. Hence, C1 upto C4 will all execute this fork statement and result in one children each, ie 4 children. Hence the total number of processes becomes 10 (P, C, C1, C2, ....C7, C8). Now the last fork() statement is executed and each of these 10 processes will spawn a child process, making the total count as 20.

- Aakash Rana April 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h> int main() { fork(); printf("1 "); fork(); printf("2 "); fork(); printf("3 "); return 0; }

- Anonymous May 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

24

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

This is how I got 24

A: fork();
B:fork() && fork() || fork()
C:fork();

A: fork() 2 processes
B: fork() 4 process
B: fork() && fork() 8 (4 parents, 4 children) processes
B: fork() && fork() || fork() here only children fork 4 + 8 = 12
C: fork() 24

- Anonymous March 14, 2012 | Flag


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