Microsoft Interview Question


Country: United States
Interview Type: In-Person




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

Every process runs under their own address space and hence no overwriting can ever happen...
So the answer is both processes will behave as if they were running in isolation..

- Hj December 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a flawed question at best. Significance of sleep might be for mutual exclusion so that they don't interfere with printing.

- aka November 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Both processes push the value of the local variable i to the shared location, without regard for what was there before. Henceforth, even though the memory updates are not atomic (as the destination address is not 32-bit or 64-bit word-aligned), the resulted value after SomeRunningFunction() finishes execution is 1000. Now, since the updates are not atomic we might expect spurious memory overwrites, but the limited range of values masks this flaw, as the high-order 16-bit word will always be 0, and it's only the low-order word that gets updated. Since the address is aligned to 16 bits, the 16-bit updates are this time atomic on the modern processors.

In this example, sleep(2000) acts effectively like a memory barrier, preventing both the compiler and the processor from excessively optimizing the memory writes. sleep(2000) is useful at reducing contention, but has no effect whatsoever on the final result. On both single core and multi core processors, the thread scheduling algorithms are able to make the context switches correctly, suspending the running threads in the middle even if in running state (as opposed to suspended, waiting for the sleep() to complete).

- Florian December 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Florian

Can sleep prohibit the compiler from optimizing. I have seen code like :

while(true) { sleep(0); }

to prevent compiler from removing the while loop completely.

But in this case I am curious if the compiler can optimize the code to something like :


void SomeRunningFunction()
{
for(int i = 1; i < 1000; ++i)
{
sleep(2000);
}
}

and setting *sharedmemoryaddress to 999 at compiler time.

- rishab99999 December 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

where is the print?

- haroud December 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Haroud, Mostly you will find the answer to 999. But I feel the intent here is to check for the possibility rather than finding the answer. Probably the guy wanted to check what you think of this code, so its kind of open-ended question.

- rishab99999 December 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

On uniprocessor sleep may act as a way of synchronizing output from the two processes but that is not necessarily true Everytime in this scenario. On multiprocessor, the two processes can run on different cores and can execute in parallel doing the same thing means they might sleep at the same time. Overall the result after each loop is non deterministic and at the end will be 1000 when both processes are done.

- gauravk.18 December 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I upvoted HJ but it wont appear as they run in isolation. One will throw a Segmentation fault :P

- Abhi March 14, 2016 | 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