Search This Blog

Monday 29 February 2016

Mindtree Interview Question?

Write a C# program to eliminate duplicates from an integer array without sorting.
Sample Input {1, 5, 6, 1, 7, 5, 7}
Sample Output {1, 5, 6, 7}

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

namespace SortIntegerArray
{
    class Program
    {
        public int[] EliminateDuplicatesAndSort(int[] a)
        {
            int temp;
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[i] > a[j])
                    {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }

            // Using Distinct() Removing duplicate
            a = a.Distinct().ToArray();

            return a;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            int[] a = { 1, 5, 6, 1, 7, 5, 7 };

            int[] b = p.EliminateDuplicatesAndSort(a);

            foreach (var c in b)
            {
                Console.Write(c);
            }

            Console.ReadKey();
        }
    }
}

OUTPUT

1, 5, 6, 7

Note:-

You can also use following code

if (a[i] > a[j])
{
     a[i] = a[i] + a[j];
     a[j] = a[i] - a[j];
     a[i] = a[i] - a[j];
}

In the place of

if (a[i] > a[j])
{
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
}


For Removing Duplicate

List<int> list = new List<int>();
foreach (int n in a)
{
        if (!list.Contains(n))
        {
             list.Add(n);
        }
}

In the place of

a = a.Distinct().ToArray();


How’s sorting loop processed?

Input       1     5     6     1     7     5     7    

Index       0     1     2     3     4     5     6

0 1
1>- F

0 2
1>- F

0 3
1>- F

0 4
1>- F

0 5
1>- F

0 6
1>- F

First Time Loop Output
1     5     6     1     7     5     7    
---------

1 2
5>- F

1 3
5>- T

Process
1     1     6     5     7     5     7

1 4
1>- F

1 5
1>- F

1 6
1> F

Second Time Loop Output
1     1     6     5     7     5     7
---------

2 3
6>- T

Process
1     1     5     6     7     5     7

2 4
5>- F

2 5
5>- F

2 6
5>- F

Third Time Loop Output
1     1     5     6     7     5     7
---------

3 4
6>- F

3 5
6>- T

Process
1     1     5     5     7     6     7

3 6
5>- F

Fourth Time Loop Output
1     1     5     5     7     6     7
---------

4 5
7>- T

Process
1     1     5     5     6     7     7

4 6
6>- F

Fifth Time Loop Output
1     1     5     5     6     7     7


Write a generic Swap method that can be used with multiple data types (for swapping Integer variables, Double variables. float variables)

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

namespace GenericSwap
{
    class Program
    {
        // Using temp variable
        public void Swap<T>(ref T arg1, ref T arg2)
        {
            T temp;
            temp = arg1;
            arg1 = arg2;
            arg2 = temp;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            int arg1 = 10, arg2 = 20;

            Console.WriteLine("Before Swapping : {0}, {1}", arg1, arg2);

            p.Swap(ref arg1, ref arg2);

            Console.WriteLine("After Swapping : {0}, {1}", arg1, arg2);

            Console.ReadKey();
        }
    }
}

OUTPUT

Before Swapping: 10, 20
After Swapping: 20, 10

Note:-

You can also use following methods to Swap

// Using Arithmetic Operators
public void Swap<T>(ref T arg1, ref T arg2)
{
       arg1 = (dynamic)arg1 + (dynamic)arg2;
       arg2 = (dynamic)arg1 - (dynamic)arg2;
       arg1 = (dynamic)arg1 - (dynamic)arg2;
}

// Using Bitwise XOR
public void Swap<T>(ref T arg1, ref T arg2)
{
       arg1 = (dynamic)arg1 ^ (dynamic)arg2;
       arg2 = (dynamic)arg1 ^ (dynamic)arg2;
       arg1 = (dynamic)arg1 ^ (dynamic)arg2;
}


Create a sample Indexer class for integer values and write C# code to show its usage.

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

namespace IndexerClass
{
    class IndexerClass
    {
        // Array of integer values
        private int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // Return Array Length
        public int Length
        {
            get
            {
                return values.Length;
            }
        }

        // Indexer declaration.
        public int this[int index]
        {
            get
            {
                return values[index];
            }

            set
            {
                values[index] = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IndexerClass p = new IndexerClass();

            // Use the indexer's set accessor
            p[1] = 22;
            p[2] = 33;

            // Use the indexer's get accessor
            for (int i = 0; i < p.Length; i++)
            {
                Console.WriteLine(p[i]);
            }

            Console.ReadKey();
        }
    }
}

OUTPUT
1
22
33
4
5
6
7
8
9
10

Note:-

Indexers allow class to be used just like an array.

Write a Palindrome algorithm to find out given argument is palindrome series or not?  

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

namespace Palindrome
{
    class Program
    {
        public string PrintPalindrome(string arg)
        {
            // Converting string to char array
            char[] a = arg.ToCharArray();

            // Reversing string using Array class Reverse() function
            Array.Reverse(a);

            // Comparing string with new string
            if (arg.ToLower() == new string(a).ToLower())
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            Console.WriteLine("It's a Palindrome number - " + p.PrintPalindrome("16461"));

            Console.ReadKey();
        }
    }
}

OUTPUT
It's a Palindrome number - Yes

Note:-

You can also use following methods to find Palindrome

        public string PrintPalindrome(string arg)
        {
            StringBuilder sb = new StringBuilder();

            // Reversing string using for loop and storing in StringBuilder
            for (int i = arg.Length - 1; i >= 0; i--)
            {
                sb.Append(arg[i]);
            }

            // Comparing string with StringBuilder
            if (arg.ToLower() == sb.ToString().ToLower())
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }

        public string PrintPalindrome(string arg)
        {
            for (int i = 0; i < arg.Length - 1; i++)
            {
                // Checking only first and last character of string
                if (arg[i].ToString().ToLower() == arg[arg.Length - 1 - i].ToString().ToLower())
                {
                    return "Yes";
                }
                else
                {
                    return "No";
                }
            }
            return "No";

        }