# Algorithms – DOT NET Chapter Wise Interview Questions

## Algorithms – DOT NET Chapter Wise Interview Questions

Question 1:
Find if number is even or odd?
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#.

Question 2:
Write a FizzBuzz program in C#.
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”.

Question 3:
Can you write a simple C# code to display Fibonacci series?
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.

Question 4:
Can you write a logic for bubble sort in C#?
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#.

Question 5:
What is inserted sort algorithm?
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.

Question 6:
How does selection sort algorithm work?
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. 