Microsoft Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

in C#:

class SerialConnection
        {
	        private Action<char> receiveByteFunction;
            bool portOpen = false;
            Thread receivingThread;
            Thread callback;
            Queue<char> queue = new Queue<char>();

            public SerialConnection(Action<char> receiveByteFunction)
            {
                this.receiveByteFunction = receiveByteFunction;
                callback = new Thread(SendToDispatcher);
                callback.Start();
            }

            ~SerialConnection()
            {
                callback.Abort();
            }

            public void StartReceiving()
            {
                if (open_port() == 0)
                {
                    portOpen = true;
                    receivingThread = new Thread(ReadUntilDone);
                    receivingThread.Start();
                }
                else
                {
                    throw new Exception("port couldn't be opened");
                }
            }

            int bufferSize = 512;

            public void Cancel()
            {
                if (portOpen)
                {
                    portOpen = false;
                    close_port();
                }
                receivingThread.Abort();
            }

            public void ReadUntilDone()
            {
                char[] buff = new char[bufferSize];
                int charReceived;

                //read all (foreach)
                charReceived = read_port(buff, bufferSize);
                while (charReceived > 0)
                {

                    for (int i = 0; i < bufferSize; i++)
                    {
                        lock (queue)
                        {
                            queue.Enqueue(buff[i]);
                            Monitor.Pulse(queue);
                        }
                    }

                    charReceived = read_port(buff, bufferSize);
                }
            }

            public void SendToDispatcher()
            {
                char toSend;
                bool hasData;

                while (true)
                {

                    lock (queue)
                    {
                        hasData = queue.TryDequeue(out toSend);
                        if (!hasData)
                            Monitor.Wait(queue);
                    }

                    if (hasData)
                    {
                        receiveByteFunction(toSend);
                    }
                }
            }
        }

- Anonymous August 02, 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