Microsoft Interview Question


Country: -




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

Use two semaphores. One as a mutex and other as a semaphore and keep a count of waiting semaphores.

Semaphore s (0, n); // Allows n requests and is initialized to 0
Semaphore m(1); // Mutex
int count;

void Barrier()
{
    m.WaitOne();
    count++;

    if (count == n)
    {
       count = 0;
       m.ReleaseOne();
       s.Release(n);   
       return;
    }
    else
    {       
       s.WaitOne();
    }
}

- Ashish Kaila December 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you have the right idea but not sure the code is right. It looks like you never actually release your mutex until the nth thread arrives. Probably should release right after incrementing your count. Unless you can't guarantee there won't be more than n threads, in which case you should release m in your else block as well.

- JoseCuervo August 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Change suggested
{
mutex_lock()
count++;
if(count<n))
{
mutex_unlock();
sem_wait();
}
else
{
sem_post();
}

}

- Pramod July 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check out the simple barrier and reusable barrier implementation from the little book of semaphores google for the pdf you can download for free from the author site

- Venkata Pavan Kumar Sannisetty March 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printf("Rocky is the best");

- rocky17291 November 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

V(s1,1);
P(s2,1);
if (rank == 0)
{
  P(s1, n);
}
V(s2, 1);

- Ashish Kumar Singh October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you understand the question? Or even worse did you understand your answer and the remotest possibility on how it could be related to the question?

- ashishkaila@hotmail.com December 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i found the sulution over here
michkashala.blogspot.com

- mubarak January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hey mubarak that solution is really gr8

http : // michkashala.blogspot.com
really helpfully
thanks a lot

- salim January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hey salim that solution is really gr8

http : // michkashala.blogspot.com


really helpfully
thanks a lot keep posting

- munna January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com


really helpfully
thanks a lot keep posting

- yakrut January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting
there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

- sabka BAAP January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting
there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting
there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting
there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

there are so many gr8 of kind articles over there

http : // michkashala.blogspot.com

really helpfully
thanks a lot keep posting

- yasin kuttawala January 05, 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