Microsoft Interview Question for Software Engineer in Tests






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

The basic structure of the reader writer problem is like

--Reader()
Wait until no writers
Access database
Check out - wake up a waiting writer

--Writer()
Wait until no active reader or writer
Access the database
Check out - wake waiting reader or writer

The detailed translation would be something like this

Reader()
{
//first check self into the system
lock.acquire();
while((AW+WW) > 0) // is it safe to read ?
{
     WR++; // No. Writers exist.
     OkToRead.wait(&lock); // sleep on condition variable
     WR--;  // No longer waiting
}
     AR++;
     lock.release();
     // perform actual read-only access
     AccessDataBase(readonly);
     //Now, check out of the system
     lock.acquire();
     AR--; // no longer active
     if(AR == 0 && WW > 0) // no other active readers
          OkToWrite.signal(); // wake up one writer
     lock.release();
}

//Logic for writer
Writer()
{
   // first check self into the system
   while((AW+AR)>0){ // Is it safe to write ?
         WW++; // No. active users exist
         OkToWrite.wait(&lock); //sleep on cond variable
         WW--; // no longer waiting
   }
   AW++; // Now we are active
   lock.release();
   // perform actual write access
   AccessDataBase(Write);
   
   // Now check out of the system
   lock.acquire();
   AW--;  // no longer active
   if(WW > 0){   // give priority to writers
         OkToWrite.signal(); // wake up one writer
   }else if(WR > 0){  // otherwise wake readers
     OkToRead.broadcast(); // wake all readers.
   }
   lock.release();
}

This problem is tough to get right and lot depends on the which all process are atomic. I think in the above solution also readers may starve
at this Reader() entry code
while((AW+WW) > 0) // is it safe to read ?

- algooz April 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oops i forgot to metion what the variables mean.
Here it goes
AR -> activer reader
WR -> waiting reader
WW -> waiting writer
AW -> Active writer

- algooz April 27, 2008 | 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