KLA Tencor Interview Question for Dev Leads Dev Leads


Country: India
Interview Type: In-Person




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

We will implement writer class having a static property : int writerCount, and a private property writerId
and will implement reader class having a static property : int readerCount and a private property readerId

Each time a writer/readerId Object is created, the object will be assigned writerId = ++writerCount, and readerId = ++readerCount


Now writeToBuffer() method in writer class checks
if(getBuffer().isEmpty() && !getBuffer().writeLocked && getBuffer().writeCount()%Writer::writerCount == writerId) {
getBuffer().acquireWriteLock();
getBuffer().write(data);
getBuffer.incrementWriteCount();
getBuffer().setFull();
getBuffer.releaseWriteLock();
}
else wait();


For reader, we will do the following:
if(getBuffer().isFull() && getBuffer().readCount()%Reader::readerCount == readerId) {
data = getBuffer().read();
getBuffer.incrementReadCount();
getBuffer().setEmpty();
}
else wait();

- laxmi.udaseen November 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey laxmi, can we first discuss the algo and then jump on to code ??

- krishna November 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static object bufferRWLock = new object();
        static object bufferFullLock = new object();
        static bool bufferFull = false;
        static byte[] buffer = new byte[1024];
        public class Writer
        {
            private string name;
            public Writer(string name)
            {
                this.name = name;
            }
            public void Write()
            {
                lock (bufferFullLock)
                {
                    if (bufferFull)
                    {
                        System.Console.WriteLine("{0} tried to write, but buffer was full", name);
                        return;
                    }
                }
                lock (bufferRWLock)
                {
                    //
                    
                    // Write to buffer here...

                    //

                    lock (bufferFullLock)
                    {
                        System.Console.WriteLine("{0} wrote to buffer", name);
                        bufferFull = true;
                    }
                }
            }
        }

        public class Reader
        {
            private string name;
            public Reader(string name)
            {
                this.name = name;
            }
            public void Read()
            {
                lock (bufferFullLock)
                {
                    if (bufferFull)
                    {
                        lock (bufferRWLock)
                        {
                            //

                            // Read the buffer and empty it here...

                            //

                            bufferFull = false;
                            System.Console.WriteLine("{0} has read the buffer", name);
                        }
                    }
                    else
                    {
                        System.Console.WriteLine("{0} tried to read, but no buffer", name);
                    }
                }
            }
        }

- Anonymous September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static object bufferRWLock = new object();
        static object bufferFullLock = new object();
        static bool bufferFull = false;
        static byte[] buffer = new byte[1024];
        public class Writer
        {
            private string name;
            public Writer(string name)
            {
                this.name = name;
            }
            public void Write()
            {
                lock (bufferFullLock)
                {
                    if (bufferFull)
                    {
                        System.Console.WriteLine("{0} tried to write, but buffer was full", name);
                        return;
                    }
                }
                lock (bufferRWLock)
                {
                    //
                    
                    // Write to buffer here...

                    //

                    lock (bufferFullLock)
                    {
                        System.Console.WriteLine("{0} wrote to buffer", name);
                        bufferFull = true;
                    }
                }
            }
        }

        public class Reader
        {
            private string name;
            public Reader(string name)
            {
                this.name = name;
            }
            public void Read()
            {
                lock (bufferFullLock)
                {
                    if (bufferFull)
                    {
                        lock (bufferRWLock)
                        {
                            //

                            // Read the buffer and empty it here...

                            //

                            bufferFull = false;
                            System.Console.WriteLine("{0} has read the buffer", name);
                        }
                    }
                    else
                    {
                        System.Console.WriteLine("{0} tried to read, but no buffer", name);
                    }
                }
            }
        }

- Issam Farran September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We will implement writer class having a static property : int writerCount, and a private property writerId
and will implement reader class having a static property : int readerCount and a private property readerId

Each time a writer/readerId Object is created, the object will be assigned writerId = ++writerCount, and readerId = ++readerCount


Now writeToBuffer() method in writer class checks
if(getBuffer().isEmpty() && !getBuffer().writeLocked && getBuffer().writeCount()%Writer::writerCount == writerId) {
getBuffer().acquireWriteLock();
getBuffer().write(data);
getBuffer.incrementWriteCount();
getBuffer().setFull();
getBuffer.releaseWriteLock();
}
else wait();


For reader, we will do the following:
if(getBuffer().isFull() && getBuffer().readCount()%Reader::readerCount == readerId) {
data = getBuffer().read();
getBuffer.incrementReadCount();
getBuffer().setEmpty();
}
else wait();

- Laxmi Udaseen, laxmi.udaseen@gmail.com November 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess this is self explanatory. @ Krishna.

- Amit October 06, 2019 | 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