Tags: algorithm

SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

QuestPond’s Interview Questions & Answers on .NET

How to write a FizzBuzz program in C#?

Ok first let me explain what is FizzBuzz, Fizz

"Write a c# program that prints numbers from 1 to 100. But for multiples of 3 print “Fizz” instead of the number and for the multiples of 5 print “Buzz”. For numbers which are multiples of both 3 and 5  print “FizzBuzz”.

for (int j = 1; j <= 100; j++)
 {
      string Output = "";

      if (j % 3 == 0) Output = "Fizz";// Divisible by 3 --> Fizz

      if (j % 5 == 0) Output += "Buzz"; // Divisible by 5 --> Buzz

      if (Output == "") Output = j.ToString(); // If none then --> number

      Console.WriteLine(Output); // Finally print the complete output
 }


Click and see here for more programming algorithm in .NET
SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

.NET interview questions: - How does selection sort algorithm works?

This is question which will really test your logic from programming perspective.

Selection sort is the most simples sorting algorithm.  It finds the lowest value from the collection and moves it to the left. This is repeated until the complete collection is sorted.
Below is a pictorial representation of how selection sort works. We have simple collection down below which has 5, 7, 1, and 0 values.

Step 1:- Find the smallest value from the collection. In the current collection the smallest value is “0”. Move this smallest value to the left hand side.

Step 2 :- Iterate again to find the next smallest value. The next smallest value is “1”. Move this value to the left hand side before the first smallest value.
In this manner continue with all elements in a collection and your list is sorted.

                    a1

Below is a simple c# code for selection algorithm.

// array of integers to hold values
privatestaticint[] a = newint[4]{2,8,0,3};

staticvoid Main(string[] args)
      {
          Sort();
foreach (int temp in a)
          {
Console.WriteLine(temp);
          }
Console.ReadLine();
      }

publicstaticvoid Sort()
      {
int i, j;
int min, temp;

for (i = 0; i < a.Count() - 1; i++) // Loop through each element
          {
              min = i; // Assume that he is the smallest

for (j = i + 1; j < a.Count(); j++) // Loop through the remaining element
              {
if (a[j] < a[min]) // If the current value smaller than the min
                  {
                      min = j; // Swap the values
                  }
              }

              temp = a[i]; // Store the current value in temp variuable
              a[i] = a[min]; // Swap the minimum  value to the current position
              a[min] = temp; // Swap the current value to the minimum value position
          }
      }

See the following .NET interview questions video on IL code, CLR, CTS, CAS:  -