Microsoft Interview Question for SDE-2s


Country: United States




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

in C#, a quick console application:

class Program
    {
        
        class ObjStatus
        {
            public status Status { get; set; }
            public int ReadCount { get; set; }
        }
        enum status
        {
            none,
            reading,
            writing
        }

        static ObjStatus objStatus = new ObjStatus();

        static void Main(string[] args)
        {
            Thread t1 = new Thread(Read);
            Thread t2 = new Thread(Write);
            Thread t3 = new Thread(Read);
            Thread t4 = new Thread(Write);
            Thread t5 = new Thread(Read);
            Thread t6 = new Thread(Write);
            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
            t5.Start();
            t6.Start();
        }

        static void Read()
        {
            //Console.WriteLine("Read starting");
            WaitForAccess(status.reading);
            Console.WriteLine("Read access confirmed");

            //do stuff
            Thread.Sleep(GetSleepTime());

            Console.WriteLine("Read ended - pulsing");

            UpdateAccess(status.reading);

            Console.WriteLine("Read ended - finished");

        }

        static void Write()
        {
            //this is only for writing
            //Console.WriteLine("Write started");

            WaitForAccess(status.writing);

            Console.WriteLine("Write access confirmed");

            //write
            Thread.Sleep(GetSleepTime());

            UpdateAccess(status.writing);

            Console.WriteLine("Write ended");
        }

        private static void UpdateAccess(status accessFinished)
        {
            lock (objStatus)
            {
                if (accessFinished == status.writing)
                {
                    objStatus.Status = status.none;
                    objStatus.ReadCount = 0;
                }
                else
                {
                    objStatus.ReadCount--;

                    if (objStatus.ReadCount == 0)
                    {
                        //we waited, we got the lock, now update
                        objStatus.Status = status.none;
                    }
                }
                Monitor.PulseAll(objStatus);
            }
        }

        private static void WaitForAccess(status accessWanted)
        {
            lock (objStatus)
            {
                if(accessWanted == status.writing)
                {
                    //wants to write, needs to be "none"
                    while(objStatus.Status != status.none)
                    {
                        Monitor.Wait(objStatus);
                    }

                    //we waited, we got the lock, now update
                    objStatus.Status = status.writing;
                    objStatus.ReadCount = 0;
                }
                else
                {
                    //wants to read.
                    //needs to be either read or none
                    while (objStatus.Status == status.writing)
                    {
                        Monitor.Wait(objStatus);
                    }

                    //we waited, we got the lock, now update
                    objStatus.Status = status.reading;
                    objStatus.ReadCount++;
                }
            }
        }

        private static int GetSleepTime()
        {
            return new Random().Next(1500)+500;
        }

    }

- Diego August 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

synchronized(write) {
synchronized(read) {

}
}

void read() {
}

- Harish September 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thread reader = new Thread(new Runnable{
    void run(){
        while(true)
        {
            synchronized(wl)
            {
                wl.wait();
                //read
            }
          
        }
    }
});
reader.start();


Thread writer = new Thread(new Runnable{
    void run(){
        while(true)
        {
            synchronized(wl)
            {
              //write
               wl.notifyAll();
            }
              
            
        }
    }
})
writer.start();

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

Thread reader = new Thread(new Runnable{
    void run(){
        while(true)
        {
            synchronized(wl)
            {
                wl.wait();
                //read
            }
          
        }
    }
});
reader.start();


Thread writer = new Thread(new Runnable{
    void run(){
        while(true)
        {
            synchronized(wl)
            {
              //write
               wl.notifyAll();
            }
              
            
        }
    }
})
writer.start();

- fitsboy December 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReadWrite {
	private int reader = 0;
	private int writer = 0;
	private int writeRequest = 0;
	
	public void read() throws InterruptedException{
		synchronized(this) {
			while(writer > 0 || writeRequest > 0) {
				wait();
			}
			
			reader++;
		}
		
		try {
			System.out.println(Thread.currentThread().getName() + " is reading");
			Thread.sleep(5L * 1000L);
			System.out.println(Thread.currentThread().getName() + " reading completed");
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		synchronized(this) {
			reader--;
			notifyAll();
		}
	}
	
	public void write() throws InterruptedException{
		synchronized(this) {
			writeRequest++;
			while(reader > 0 || writer > 0) {
				wait();
			}
			
			writeRequest--;
			writer++;
		}
		
		try {
			System.out.println(Thread.currentThread().getName() + " is writing");
			Thread.sleep(5L * 1000L);
			System.out.println(Thread.currentThread().getName() + " write completed");
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		synchronized(this) {
			writer--;
			notifyAll();
		}
	}
}

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

package threads;

public class ReadWrite {
	private int reader = 0;
	private int writer = 0;
	private int writeRequest = 0;
	
	public void read() throws InterruptedException{
		synchronized(this) {
			while(writer > 0 || writeRequest > 0) {
				wait();
			}
			
			reader++;
		}
		
		try {
			System.out.println(Thread.currentThread().getName() + " is reading");
			Thread.sleep(5L * 1000L);
			System.out.println(Thread.currentThread().getName() + " reading completed");
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		synchronized(this) {
			reader--;
			notifyAll();
		}
	}
	
	public void write() throws InterruptedException{
		synchronized(this) {
			writeRequest++;
			while(reader > 0 || writer > 0) {
				wait();
			}
			
			writeRequest--;
			writer++;
		}
		
		try {
			System.out.println(Thread.currentThread().getName() + " is writing");
			Thread.sleep(5L * 1000L);
			System.out.println(Thread.currentThread().getName() + " write completed");
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		synchronized(this) {
			writer--;
			notifyAll();
		}
	}
}

- Anonymous August 13, 2021 | 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