SoFunction
Updated on 2025-03-06

Summary of interview questions for common algorithms in C#

This article summarizes common algorithm questions and answers for C# interviews. It has good learning and reference value. Share it for your reference. The details are as follows:

1. Write out the bubbles, select, and insert the sorting algorithm.

  //Bubbling sort  public class bubblesorter
  {
    public void sort(int[] list)
    {
      int i, j, temp;
      bool done = false;
      j = 1;
      while ((j < ) && (!done))
      {
        done = true;
        for (i = 0; i <  - j; i++)
        {
          if (list[i] > list[i + 1])
          {
            done = false;
            temp = list[i];
            list[i] = list[i + 1];
            list[i + 1] = temp;
          }
        }
          j++;
      }
    }
  }
  //Select sort  public class selectionsorter
  {
    private int min;
    public void sort(int[] list)
    {
      for (int i = 0; i <  - 1; i++)
      {
        min = i;
        for (int j = i + 1; j < ; j++)
        {
          if (list[j] < list[min])
            min = j;
        }
        int t = list[min];
        list[min] = list[i];
        list[i] = t;
      }
    }
  }
  //Insert sort  public class insertionsorter
  {
    public void sort(int[] list)
    {
      for (int i = 1; i < ; i++)
      {
        int t = list[i];
        int j = i;
        while ((j > 0) && (list[j - 1] > t))
        {
          list[j] = list[j - 1];
          --j;
        }
        list[j] = t;
      }
    }
  }

2. There is a column of numbers 1, 1, 2, 3, 5,... Find the 30th number.

public class MainClass
{
  public static void Main()
  {
    (Foo(30));
  }
  public static int Foo(int i)
  {
    if (i <= 0)
      return 0;
    else if (i > 0 && i <= 2)
      return 1;
    else return Foo(i - 1) + Foo(i - 2);
  }
}

3. Programming: The cat screamed, all the mice began to escape, and the owner was awakened.

  public delegate void SubEventHandler(); 
  public abstract class Subject 
  { 
    public event SubEventHandler SubEvent; 
    protected void FireAway() 
    { 
      if ( != null) 
        (); 
    }  
  } 
  public class Cat : Subject 
  { 
    public void Cry() 
    { 
      (cat cryed.); 
      (); 
    } 
  } 
  public abstract class Observer 
  { 
    public Observer(Subject sub) 
    { 
       += new SubEventHandler(Response); 
    } 
    public abstract void Response();  
  } 
  public class Mouse : Observer 
  { 
    private string name; 
    public Mouse(string name, Subject sub) : base(sub) 
    {  
       = name; 
    } 
    public override void Response() 
    { 
      (name + attempt to escape!); 
    } 
  } 
  public class Master : Observer 
  { 
    public Master(Subject sub) : base(sub){} 
    public override void Response() 
    { 
      (host waken); 
    } 
  } 
  class Class1 
  { 
    static void Main(string[] args) 
    { 
      Cat cat = new Cat(); 
      Mouse mouse1 = new Mouse(mouse1, cat); 
      Mouse mouse2 = new Mouse(mouse2, cat); 
      Master master = new Master(cat); 
      (); 
    } 
  } 

4. There is a string "I am a good man", design a function, and returns "man good a am I".

static string Reverse() 
{ 
 string s = "I am a good man"; 
 string[] arr = (' '); 
 string res = ""; 
 for (int i =  - 1; i >= 0; i--) 
 { 
   res += arr[i]; 
   if (i > 0) 
  res += " "; 
 } 
 return res; 
}

5. Five students, A, B, C, D, and E, may participate in the computer competition. According to the following conditions, who participated in the competition:

(1) When A participates, B also participates;

(2) Only one person from B and C participated;

(3) Both C and D participate, or do not participate;

(4) At least one of D and E participated;

(5) If A participates, then both A and D participate.

static void Main(string[] args)
{
  char[] name={'A','B','C','D','E'};
  int[] value = new int[5];
  for (value[0]=0;value[0]&lt;2;value [0]++)
 for (value[1]=0; value[1] &lt; 2; value[1]++)
   for (value[2]=0; value[2] &lt; 2; value[2]++)
 for (value[3]=0; value[3] &lt; 2; value[3]++)
   for (value[4]=0; value[4] &lt; 2; value[4]++)
   {
  if ((value[1] &gt;= value[0]) &amp;&amp; (value[1] + value[2] == 1) &amp;&amp; (value[2] == value[3]) &amp;&amp; (value[3] + value[4]==1) &amp;&amp; (value[4]==0 || value[4]==1 &amp;&amp; value[0]==1 &amp;&amp; value[3]==1))
  {
    for (int i = 0; i &lt; 5; i++)
    {
  if (value[i]==1)
  {
    ("{0}join", name[i]);
  }
  else
  {
    ("{0}不join", name[i]);
  }
    }
  }
   }
}

6. Question:
a user entered an integer value into a text box. Without using a buit-in library, convert the numeric string to its integer representation.

static int StringTolnt(string s)
{
  int sum = 0;
  for (int i = 0; i < ; i++)
 sum = sum * 10 + (s[i] - '0');
  return sum;
}

I believe that the description in this article has certain reference value for everyone's C# programming.