Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

The answer is 8 times two and 8 times one.. How ..? see below..!

after 1 st call to fork() -> there are 2 processes [printf not executed till now]
after 2 nd call to fork() -> there are 4 processes [printf not executed till now]
after 3 rd call to fork() -> there are 8 processes [printf not executed till now]

Till the time we have called fork() is called thrice which resulted into creating 8 processes. Now we are going to call 4 th fork().
after 4 th call to fork() -> now there are 16 processes...

Now we are going to print. This print will be executed for all 16 processes out of them 8 processes will print one and 8 processes will print two..

The order of printing ones ant twos is not guaranteed. it depends on CPU scheduling.

- f2003062 July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
5
of 5 vote

growing binary tree....8 time one and 8 time two (2^n where n= number of fork() call ).

- govind.chauhan143 June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

There will be maximum of 16 processes active. Each parent when ending will print one and each child two. A parent is any process that spawned another and a child is a process that did not call "fork"
So in theory you would get any series of 8 times "one" and 8 times "two" and we can never have more two's then one's in the series.

That being said no father process waits for the children to end so the process might (and it probably will) end without all the processes being executed.
So the correct answer is we will get a series of up to 8 "one" and up to 8 "two" where the only sure thing is there will not be more twos than ones if we look from 1st row to last

- badescudan July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

8 times two seven times one
a complete binary tree of 4 levels have 15 nodes
so 7 ones and 8 twos....

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

can you explain why it will be like this
thanks

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

yup....as we know that fork is used to create the child process
(which p.t to parent next step instruction ) duplicate of parent process plus we can't which process while get first Machine it is depends on CPU .

if progm content 1 fork then there is 2 process.
if progm content 2 fork then there is 4 process.
if progm content 3 fork then there is 8 process.
if progrm content 4 fork then there is 16 process.

- govind.chauhan143 June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

total 2^n processes means 16 processes
out of which 1 is main rest all are child
for parent fork return greater than 0 negation in if means two will be printed once 
and all rest child means that fork will return 0 for them means one will be printed
o/p    two...............15 times one..................

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

Could someone explain why it should be a binary tree?

I think it will be a general tree , because with every fork(), the previous running process should get a child process.. so 'main' process(root of our tree) should have 4 children and so on...which is the only way we get a total of 16 nodes-> 16 processes. And we cannot decide the order of 1s and 2s.. it will depend on the CPU scheduling..

- ramblersm July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ANS is 15

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

but on ideone.com the following codes gives output as
two
one
two
two
two


just the 5 not 16 prints why can anybody suggest.

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

sorry not following but I m talking about the given code

- NNavneetsinha August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Every time i am running this code on ideone.com , it is giving different outputs, Can someone explain why ??
It never gives 16 print statements :(

- WeAreBack November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Every time i am running this code on ideone.com , it is giving different outputs, Can someone explain why ??
It never gives 16 print statements :(

- WeAreBack November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think output will be
one
two
one
two
........like this total 8 times

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

The output will contain total 8 one and total 8 two printed.
But the order in which they would be printed would depend highly whether parent or child process get scheduled first.

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

In my above solution i have considered that child process will get executed first always which is not the case always

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

In fact, could the "one"s and "two"s be garbled? The child processes could be operating on separate cores and the prints could be happening simultaneously, right? (or even if they were on just one core, can a process be preempted in the middle of a printf?)

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

@vogelmanj ...right expln.

- govind.chauhan143 June 30, 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