Microsoft Interview Question for Software Engineer / Developers






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

How did you solve part C?

- Swati February 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

part A:

// initialize 2 semaphores:
sem A_done(0);
sem B_done(0);

//let's say methods are declared as follows:

Foo::A() {
   create_thread(func_A);
}

Foo::B() {
   create_thread(func_B); 
}

Foo::C() {
   create_thread(func_B); 
}

// add the following sync code to thread functions:

func_A() {
  /* body */
 
   A_done.signal(); // increments semaphore variable by 1 
                  // (and unblocks one thread out of those waiting on this semaphore) 
}

func_B() {
   A_done.wait(); // waits on semaphore
   
   /* body */
   B_done.signal();
}

func_C() {
   B_done().wait();
    
   /* body */
}

part B:

// declare 3 semaphores:
sem A_done(0), B_done(0), C_done(1);

// function bodies:

func_A() {
   C_done.wait();
  /* body */
 
   A_done.signal(); // increments semaphore variable by 1 
                  // (and unblocks one thread out of those waiting on this semaphore) 
}

func_B() {
   A_done.wait(); // waits on semaphore
   
   /* body */
   B_done.signal();
}

func_C() {
   B_done().wait();
    
   /* body */
   C_done.signal();
}

part C 3) didn't quite understand the question

- pavel.em June 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do we actually need semaphores here for part A since threads share the memory? My thought is declaring a global variable and each thread will set it and the following thread won't start until it sees the value has been changed by the previous thread? The cost is busy waiting though.

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

reading and writing a shared variable with threads without mutex's or synchronization is a no no in thread programming. At least in the Unix world, I do not think they would enjoy that answer!

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

part C:

2 possible sollutiins:

#1 use WaitForSingleObject function with 0 timeout. or as in POSIX pthread_mutex_trylock. It will be a non waiting solution but the program will have to perform polling

#2 create a queue with requests. Then the caller leaves its request in the queue and returns. A separate thread is checking the queue periodically.

- kykydyk March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A correction. one more mutex is needed to prevent two Thread B from running at the same time.
in fun_b, wait also for this mutex, and only release this mutex in fun_a.

- Jackie September 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Great Q and a good answer to understand/design thread based solutions.

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

one of th best threading questions asked...did they ask this in Microsoft?What position?

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

one of the best threading questions asked...did they ask this in Microsoft?What position?

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

one of the best threading questions asked...did they ask this in Microsoft?What position?

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

one of the best threading questions asked...did they ask this in Microsoft?What position?

- Anonymous January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about the part C? anyone?

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

what about the part C? anyone?

- Anonymous February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

one of the best threading questions asked...did they ask this in Microsoft?What position?

- Anonymus December 13, 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