## Algorithms – DOT NET Chapter Wise Interview Questions

**Question 1: **Find if number is even or odd?

**Answer:**

A number is even if remainder yields zero when divided by “2”. A number is odd when remainder yields zero, if divided by “3″. You get the remainder value by using the “%” sign.

Below is a simple code which shows how to check for even and odd number in C#.

1 2 3 4 5 6 7 8 9 10 | Console.WriteLine("Enter your number"); int num = Convert.ToIntl6(Console.ReadLine()); if (num % 2 == 0) //Is the remainder zero { Console.WriteLine("Even number"); } else { Console.WriteLine("Odd number"); } |

**Question 2: **Write a FizzBuzz program in C#.

**Answer:**

Ok first let me explain what are 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”.

1 2 3 4 5 6 7 8 | 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 } |

**Question 3: **Can you write a simple C# code to display Fibonacci series?

**Answer:**

Let’s first to understand what exactly is Fibonacci series. Fibonacci series are nothing but series of numbers 0, 1, 1,2, 3, 5 and so on.

The specialty of this series is that next number is addition of previous two numbers. Figure 21.1 explains how Fibonacci series works. We first start with 0 and 1. The next number is addition of “0 + 1” which will give us “1”. The next number will be again addition of previous value which is “1” and the current value which is “1” which will give us “2” and so on.

Implementing Fibonacci series in C# is a four-step process. Figure 21.2 shows the same in a pictorial format.

**Step 1:**Define 3 variables “Prev”, “Next” and “Sum”. Initialize these variable with “Prev=1”, “Next = -1” and “Sum=0”.

**Step 2:**Add “Prev” and “Next” variable to get the Sum. Display the “Sum” variable.

**Step 3:**Swap “Prev” value to “Next” and “Sum” to “Previous”.

**Step 4:**Execute “Step 2” and “Step 3” again to get the next Fibonacci value.

Below is how the code will look like. Do follow the steps in comment.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //Step 1 Three variables Prev, Next and Sum // initialized with 1 and -1 values int Prev = 1; int Next = -1; int Sum = 0; // How many fibonacci numbers you want to print Console.WriteLine("How many fibonacci numbers you want to print?"); int numbers = Convert.ToIntl6(Console.ReadLine()); for (int i = 0; i < numbers; i++) { // Step 2: Add the previous and next numbers Sum = Prev + Next; Console.WriteLine(Sum); // Step 3: Swap Prev to next and Sum to Prev Next = Prev; Prev = Sum; // Step 4: Run Step 2 and Step 3 again } |

**Question 4: **Can you write a logic for bubble sort in C#?

**Answer:**

Bubble sort is a sorting algorithm which steps through a collection repeatedly, comparing and swapping until the collection gets sorted. Figure 21.3 a simple pictorial representation of how bubble sort works. We have a simple number collection of values 5, 1, 3 and 4 and we would like to sort them in an ascending manner.

**Step 1:**So we first start with number 5. The adjacent number to 5 is 1. So we compare 5 with 1. Now 5 is greater than 1 so we swap position of 1 with 5 and 5 with 1. Look at Step 2 for the final image after swapping.

**Step 2:**Now we compare 5 with 3. 5 is greater than 3 so we swap position of 5 with 3 and 3 with 5. This continues until 5 has been compared with all numbers in the list. Step 4 is the final position for number 5.

Now we take the second number, i.e., 1 and we compare and swap 1 with all other numbers until it reaches its final position. This is done with all numbers in the collection.

Below is thecode for bubble sort in C#.

1 2 3 4 5 6 7 8 9 10 11 12 13 | static public List<int> bubblesort(List<int> unsorted) { int temp; for (int i = 1; i <= unsorted.Count; i++) for (int j = 0; j < unsorted.Count - i; j++) if (unsorted[j] > unsorted[j + 1]) { temp = unsorted[j]; unsorted[j] = unsorted[j + 1]; unsorted[j +1] = temp; } return unsorted; } |

**Question 5: **What is inserted sort algorithm?

**Answer:**

In inserted sorted algorithm we compare the current element value with the previous element value. If the current value is smaller than the previous value we swap. This continues until the complete list is sorted.

Let us try to understand the same by using the below diagram. In the Figure 21.4 you can see we have an unsorted iist with values 6, 1, 3, 2.

**Step 1:**So we start with the first element i.e. “6”.There is no element before 6, so we leave it and we start from 1. We compare “6” and “1”. “1” is smaller than “6”, so we swap.

**Step 2:**Now we move to the next element “3”. Is “3” smaller than “6”, yes. So we again swap.

**Step 3:**Now we compare “6” with the next element “2”. “2” is smaller so we swap again. There are no more further elements which can be compared with “6”. So the “6” value iteration stops here.

**Step 4:**Now we take the next element “1”. There is no element before “1” so we move ahead. We move to the next element “3”. Is “3” smaller than “1”, no, so things are left where they are. So the iteration for “1” is done.We then move to the next element “3”. We compare “3” with “2”. “2” is smaller than “3” so we swap. Now “3” is compared with the next element “6”. “3” is smaller than “6” so the elements are left where they are.

**Step 5:**Now we take the last element “2”. “2” element is larger is than “1”, so “2” and “1” stay where they are. The complete collection is now sorted.

Below is a simple C# inserted algorithm code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ClassProgram { // array of integers which holds unsorted value. Privatestaticint[] UnSortedArray = newint[4]{6, 1, 3, 2}; // Insertion Sort Algorithm Staticvoid Main(string[] args) { sortArray(); foreach (int i in UnSortedArray) { Console.WriteLine(i) ; } Console.ReadLine() ; } publicstaticvoid sortArray() { int MainLoop; // This variable will helps us loop through all the elements int InnerLoop; // This variable will help us loop through all elements for every value int Currentvalue; for (MainLoop = 1; MainLoop < UnSortedArray.Count(); MainLoop++) // loop through all the elements in the array. { CurrentValue = UnSortedArray[MainLoop]; // Take the current value InnerLoop = MainLoop; // Loop through all elements for that value while ((InnerLoop > 0) && (UnSortedArray[InnerLoop - 1] > CurrentValue)) { //if the previous value is greater than currentvalue swap UnSortedArray[InnerLoop] = UnSortedArray[InnerLoop - 1]; InnerLoop = InnerLoop - 1; } // If previous value is not greater then just keep the value where they are. UnSortedArray[InnerLoop] = Currentvalue; } } |

**Question 6: **How does selection sort algorithm work?

**Answer:**

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.

Figure 21.5 shows 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.

Below is a simple C# code for selection algorithm.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // 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 he is the smallest { min = i; // Assume that for (j = i + 1; j < a.Countf); 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 } } |