Top Ad unit 728 × 90

random

C Sharp algorithm interview questions and answers


c# algorithm interview questions and answers Part 1


1) Question : Given two strings. U has to find the second string as a substring of first string. If the string is found as substring of first string then return the index where it started in the first string otherwise return -1.
   


Write Input “Internet” and search string is “net”.     
           Out put : 5    

Code is below :




//With Functions
public static void Main()
    {
        string mainStr = Console.ReadLine();
        string subStr = Console.ReadLine();
        for (int i = 0; i <= mainStr.Length - subStr.Length; i++)
        {
            if (mainStr.Substring(i, subStr.Length).Equals(subStr))
                Console.WriteLine(i.ToString());
        }
        Console.ReadLine();
   }
//Without Function
public static void Main()
    {
        string mainStr = Console.ReadLine();
        string subStr = Console.ReadLine();
        for (int i = 0; i <= mainStr.Length - subStr.Length; i++)
        {
            bool flag = true;
            for (int j = 0; j < subStr.Length; j++)
            {
                if (mainStr[i + j] == subStr[j])
                {
                    flag = flag && true;
                }
                else
                {
                    flag = flag && false;
                }               
            }
            if (flag)
            {
                Console.WriteLine(i.ToString());
            }
        }
        Console.ReadLine();
    }



Result : 



C Sharp algorithm interview questions and answers























2). Question : Find 2nd Maximum Number in an array?

public static void Main()
    {
        int[] ar = { 31, -41, 26, 59, -53, 58, 97, -93, -23, 84 };
        int max1 = 0, max2 = 0;
        for (int i = 0; i < ar.Length; i++)
        {
            if (ar[i] > max1 && max2 <=max1)
            {
                max2 = max1;
                max1 = ar[i];
            }
            else if(ar[i]>max2)
            {
                max2 = ar[i];               
            }           
        }
        Console.WriteLine("First Max  : {0} and Second max in array : {1} in array", max1, max2);
    }

Out put window :
C Sharp algorithm interview questions and answers










3). Question :  Find 3rd  max in an array?

public static void Main(string[] args)
        {
            int s=0, l=0, m=0;
            int[] ar ={ 12, 3, 34, 5, 3, 0, 19, 19 };
            for (int i = 0; i < ar.Length; i++)
            {
                if (l < ar[i] && m <= l)
                {
                    s = m;
                    m = l;
                    l = ar[i];
                }
                else if (m < ar[i] && s <= m)
                {
                    s = m;
                    m = ar[i];
                }
                else if (s <= ar[i])
                {
                    s = ar[i];
                }
            }
        }

4). Question :  Finding max sub series sum in an given array?

public static void Main()
    {
        int[] ar = { 31, -41, 26, 59, -53, 58, 97, -93, -23, 84 };
        int startIndex = 0, endIndex = 0, maxTillHere = 0, maxSoOn = 0, countStart = -1;
        for (int i = 0; i < ar.Length; i++)
        {
            if (maxTillHere + ar[i] > 0)
            {
                maxTillHere += ar[i];
                countStart++;
            }
            else
            {
                maxTillHere = 0;
                countStart = -1;
            }           
            if (maxSoOn < maxTillHere)
            {
                maxSoOn = maxTillHere;
                endIndex = i;
                startIndex = endIndex - countStart;
            }          
        }
        Console.WriteLine("Sum of max Series : " + maxSoOn + "\nStart index : {0} and End index : {1} in array" , startIndex , endIndex);
    }

5). Question : Write a function to reverse a string.
   

public static void Main()
    {
        string str = Console.ReadLine();
        char[] str1 = str.ToCharArray();
        for (int j = 0; j < str1.Length / 2; j++)
        {
            char temp;
            temp = str1[j];
            str1[j] = str1[str1.Length - j - 1];
            str1[str1.Length - j - 1] = temp;
        }
        for (int i = 0; i < str1.Length; i++)
            Console.Write(str1[i]);
        Console.ReadLine();
    }

6). Question : Find the duplicate in a given string and return the                                             string without duplicates. 

public static void Main()
    {
        int k = 1;
        string str = Console.ReadLine();
        char[] str1 = str.ToCharArray();
        for (int j = 1; j < str1.Length; j++)
        {
            bool flag = false;
            for(int i=j-1;i>=0;i--)
            {
                if(!flag && str1[i]==str1[j])
                flag=true;
            }
            if (!flag)
            {
                str1[k] = str1[j];
                k += 1;
            }
        }
        for (int i = 0; i < k; i++)
            Console.Write(str1[i]);
        Console.ReadLine();
    }

        7). Question :Write Code for binary search algorithm. If u                                                      pass unsorted array to it will it work?        
  

static void Main(string[] args)
        {
            Console.WriteLine("Enter Item Value to search(1 to 9)");
            int item = Int32.Parse(Console.ReadLine());
            int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int begin = 0;
            int end = a.Length - 1;
            int mid = (int)((begin + end) / 2);
            while (a[mid] != item && begin <= end)
            {
                if (item > a[mid])
                {
                    begin = mid + 1;
                }
                else
                {
                    end = mid - 1;
                }
                mid = (int)((begin + end) / 2);
            }
            if (a[mid] == item)
            {
                Console.Write(mid.ToString());
            }
            else
            {
                Console.Write("Item Not found");
            }
        }




8). Question :Write a program which would reverse a sentence. Test                                       cases for the program.




static void Main(string[] args)
        {
            string str = "the world will go on forever";                      
            string[] sArray= str.Split(' ');
            str = null;
            for(int i=sArray.Length-1;i>=0;i--)
                str+=sArray[i]+" ";
            Console.WriteLine("\n"+str);
        }
static void Main(string[] args)
        {
            string strSupplied = "how are you";
            string strWord = null;
            string strResult = null;
            for (int i = strSupplied.Length - 1; i >= 0; i--)
            {
                if (strSupplied[i].ToString() != " ")
                    strWord = strSupplied[i] + strWord;
                else
                {
                    strResult = strResult + " " + strWord;
                    strWord = "";
                }
            }
            strResult = strResult + " " + strWord;
            Console.Write(strResult.ToString());
            Console.ReadKey();
        }




    9). Question :Program to Convert Numeric String to Float.



static void Main(string[] args)
            {
                  string str1=”123.345”;
                  char [] str=str1.ToCharArray();
                  float num=0;
                  int flag=0,count=1;
                 
                  for (int i=0;i
                  {
                        if(str[i]=='.') flag=1;
                        else
                        {
                              num=(float)(num*10)+((str[i])-'0');
                              if(flag==1)count*=10;
                        }    

                  }
                  num = num/count;
                  Console.WriteLine(num);
            }





      10). Question : Program to Convert Int to String

     static void Main(string[] args)
        {
            int x = 123, y;
            string str = null;
            while (x > 0)
            {
                y = x % 10;
                str = str + y.ToString();
                x = x / 10;
            }
            Console.WriteLine(str);
        }


    11). Question :Checking for Palindrome

    void palind(string str1)
    {
      bool flag=true;  
      for (int i=0;i
      {
            if(str1[i]!=str1[str1.Length-i-1])
{
flag=false;
 break;
}
      }          
      if(flag)
Console.WriteLine("\nstring is palindrome\n");
      else
Console.WriteLine("\nNot palind\n");
   }
  public class palindram
    {
       public static void Main(string[] args)
        {
            int y,s=0,z;
            string st1=Console.In.ReadLine();
            int x = Convert.ToInt32(st1);
            z = x;
            while (x != 0)
            {
                y = x % 10;
                x = x / 10;
                s = s * 10 + y;
            }
            if (s == z)
                Console.WriteLine("Palindram");
            else
                Console.WriteLine("Not a palindram");
       }
    }


12). Question :Sorting the string

     
                                              
       void strSort(char[] str)
    {
      char sTemp;
      bool flag;
      for(int i=0;i
      {
            flag=false;
            for(int j=0;j
            {
                  if(str[j]>str[j+1])
                  {
                        sTemp =str[j];
                        str[j]=str[j+1];
                        str[j+1]=sTemp;
                        flag=true;
                  }
            }
            if(!flag) break;
      }     
      for(int i=0;i
      Console.Write(str[i]);
    }


 13). Question :Swapping of 2 number without using 3rd variable


class Interchange
    {
        static void Main(string[] args)
        {
            int x = 12, y=20;
            Console.WriteLine("x::::" + x);
            Console.WriteLine("y::::" + y+"\n\n");
            x = x + y;
            y = x - y;
            x = x - y;
            Console.WriteLine("x::::"+x );
            Console.WriteLine( "y::::" + y);
        }
    }

14). Question :Program for Fibonacci series.

    class Fibonacci
    {
        static void Main(string[] args)
        {
            int x = 0, y = 1,s;
            Console.Write(x + "\t" + y + "\t");
            for (int i = 0; i < 20; i++)
            {
                s = x + y;
                x = y;
                y = s;
                Console.Write(s + "\t");
            }
        }
    }

15). Question :1.      Sorting Array with 0’s and 1’s.

static void Main(string[] args)
        {
            int k = 0;
            int[] a = { 1, 0, 0, 1, 0, 0, 1, 1, 0, 0 };
            for (int i = 0; i < a.Length ; i++)
            {
                if (a[i] == 0)
                {
                    int temp = a[k];
                    a[k] = a[i];
                    a[i] = temp;
                    k++;
                }
            }           
            Console.Read();
        }
.

For moredetails about java script you can use below site.
References |  https://en.wikipedia.org/wiki/List_of_algorithms


Tag | c# programming interview questions for experienced,c# interview programming test,c# interview coding exercises,c# interview programs for freshers,c# interview programs with answers pdf,c# logical programs for interview,c# interview questions,c# logical interview questions


Go for  Client side Multiple parameter post :




C Sharp algorithm interview questions and answers Reviewed by Vikas Kumar Singh on March 20, 2018 Rating: 5

No comments:

All Rights Reserved by DotNetBasic.com © 2019

Contact Form

Name

Email *

Message *

© 2018 All rights reserved by DotNetBasic. Powered by Blogger.