Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Friday, May 25, 2012

C# .Net Interview Questions


        1. Which is better - singly linked list or doubly linked list? What are the pros and cons of using each?
Answer -
 It depends on the requirement.Single linked list has smaller memory requirement because it needs only forward reference. If you need to get next item from a specified item, then you should use single list. It can be traversed in forward direction only.

When you need to get previous or next item from a specified item then doubly linked is the best option. It can be traversed in both directions.

2. What's the difference between Managed code and Native code? 
Answer - 
Managed Code - Managed code is the code which runs under .Net CLR.  CLR basically take care of Exception handling, memory management, Code access security, etc.  It is not compiled directly to machine code , but to an intermediate language.  

Native code: Code which is complied directly to machine code. It is executed by OS directly. It always has chances of memory leak if developer has not handled it properly. Unlike CLR, There is no garbage collection and no memory management.

       3. What is thread safe code?
Answer -
In multithreaded application where multiple threads call the same method of the object. Then there should be synchronization mechanism so that one thread can not interrupt other thread. If this situation is not synchronized, then one thread might interrupt another thread and the object could be left in an invalid state. A class whose members are protected from such interruptions is called thread-safe.



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();
        }
    }
  
}