Friday, May 25, 2012


C# Interview Questions & Answers


Question 1:  Write a function that receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error). Generate test cases for the function assuming another developer coded the function.

Answer : 

int GetCubicType(int Height, int Width, int Length)
    {
        if (Height < 0 || Width < 0 || Length < 0)
            return 4;
        else
        {
            if ((Height == Width) && (Height == Length))

                return 3;
            else
                if ((Height == Width) || (Width == Length) || (Length == Width))

                    return 2;
                else
                    return 1;
        }
   }


Question 2: Implement a circular queue of integers of user-specified size using a simple array. Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe.

Answer :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThreadSafeCirculurQueue
{
    class ThreadSafeCircularQueue
    {
        private int[] _queue;
        private int _length;
        private int _head;
        private int _tail;

        private static object _objLocker = new object();

        public ThreadSafeCircularQueue()
        {
            Initialize();
        }

        private void Initialize()
        {
            _head = -1;
            _tail = -1;

            Console.Write("Please enter queue size: ");

            string length = Console.ReadLine();

            int queueLength = 0;

            bool success = int.TryParse(length, out queueLength);

            if(!success){
                Console.Write("Invalid Data\n PleaseTry Again");
                return;
            }

            _length = queueLength;

            _queue = new int[_length];
        }

        private void Enqueue(int data)
        {
            lock (_objLocker)
            {
                if ((_head == 0 && _tail == _length - 1) || 
                   (_tail + 1 == _head))
                {
                    Console.WriteLine("Queue is full.");
                    return;
                }
                else
                {
                    if (_tail == _length - 1)
                        _tail = 0;
                    else
                        _tail++;

                    _queue[_tail] = data;

                    Console.WriteLine("Inserted Item  --> {0}", data);
                }

                if (_head == -1)
                    _head = 0;
            }
        }

        private void Dequeue()
        {
            lock (_objLocker)
            {
                int data;
                if (_head == -1)
                {
                    Console.WriteLine("Queue is empty.");
                    data = -1;
                }
                else
                {
                    data = _queue[_head];
                    _queue[_head] = 0;

                    if (_head == _tail)
                        _head = _tail = -1;
                    else
                        if (_head == _length - 1)
                            _head = 0;
                        else
                            _head++;

                    Console.WriteLine("Removed Item --> {0}", data);
                }
                
            }
        }

        private void ShowData()
        {
            lock (_objLocker)
            {
                int i;
                if (_head == -1)
                {
                    Console.WriteLine("Queue is empty.");
                    return;
                }
                else
                {
                    if (_tail < _head)
                    {
                        for (i = 0; i <= _length - 1; i++)
                            Console.Write("{0} ", _queue[i]);
                    }
                    else
                    {
                        for (i = 0; i <= _tail; i++)
                            Console.Write("{0} ", _queue[i]);
                    }
                    Console.WriteLine();
                }
            }
        }

        public void EnqueueDequeue()
        {
            for (int i = 1; i < 5; i++)
            {
                Enqueue(i);
            }
            ShowData();
            Enqueue(5); // Circular queue is full!
            for(int i = 1; i <5;i++)
            {
                Dequeue();
            }

            Dequeue(); // Circular queue is empty!
            ShowData();

            Enqueue(6);
            ShowData();
            Enqueue(7);
            ShowData();
            Dequeue();
            Dequeue();
            Enqueue(8);
            ShowData();
            Enqueue(9);
            ShowData();
            Enqueue(10);
            ShowData();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ThreadSafeCircularQueue circularQueue = 
                         new ThreadSafeCircularQueue();

            Thread[] threads = new Thread[1]; // Thread can be increased

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = 
                new Thread(new ThreadStart(circularQueue.EnqueueDequeue));
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }

            Console.ReadLine();
        }
    }
  
}


No comments:

Post a Comment