Top 21 C# Coding Questions for .
NET Developer Interviews
1. Write a program to reverse a string without using in-built functions.
string input = "hello";
string reversed = "";
for (int i = [Link] - 1; i >= 0; i--)
{
reversed += input[i];
}
[Link](reversed);
2. Check if a number is Prime.
int num = 29, isPrime = 1;
for (int i = 2; i <= [Link](num); i++)
{
if (num % i == 0)
{
isPrime = 0;
break;
}
}
[Link](isPrime == 1 ? "Prime" : "Not Prime");
3. Find the factorial of a number using recursion.
int Factorial(int n)
{
return n <= 1 ? 1 : n * Factorial(n - 1);
}
[Link](Factorial(5));
4. Count the number of vowels and consonants in a string.
string str = "hello world";
int vowels = 0, consonants = 0;
foreach (char c in [Link]())
{
if ("aeiou".Contains(c)) vowels++;
else if ([Link](c)) consonants++;
}
[Link]($"Vowels: {vowels}, Consonants: {consonants}");
5. Find the second largest element in an array.
int[] arr = {5, 7, 2, 9, 1};
int first = [Link], second = [Link];
Page 1
Top 21 C# Coding Questions for .NET Developer Interviews
foreach (int n in arr)
{
if (n > first)
{
second = first;
first = n;
}
else if (n > second && n != first)
{
second = n;
}
}
[Link]($"Second Largest: {second}");
6. Check if a number or string is Palindrome.
string input = "madam";
bool isPalindrome = true;
for (int i = 0; i < [Link] / 2; i++)
{
if (input[i] != input[[Link] - i - 1])
{
isPalindrome = false;
break;
}
}
[Link](isPalindrome ? "Palindrome" : "Not Palindrome");
7. Print Fibonacci series up to N terms (recursive and iterative).
// Iterative
int n = 10, a = 0, b = 1;
[Link]($"{a} {b} ");
for (int i = 2; i < n; i++)
{
int c = a + b;
[Link]($"{c} ");
a = b;
b = c;
}
// Recursive
void Fib(int x, int y, int count)
Page 2
Top 21 C# Coding Questions for .NET Developer Interviews
{
if (count == 0) return;
int z = x + y;
[Link]($"{z} ");
Fib(y, z, count - 1);
}
Fib(0, 1, 8);
8. Sort an array using Bubble Sort.
int[] arr = {5, 3, 8, 4, 2};
for (int i = 0; i < [Link] - 1; i++)
{
for (int j = 0; j < [Link] - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]([Link](", ", arr));
9. Find duplicate elements in an array.
int[] arr = {1, 2, 3, 2, 4, 5, 1};
HashSet<int> seen = new HashSet<int>();
HashSet<int> duplicates = new HashSet<int>();
foreach (int num in arr)
{
if () [Link](num);
}
[Link]([Link](", ", duplicates));
10. Write a program to find the GCD of two numbers.
int a = 60, b = 48;
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
Page 3
Top 21 C# Coding Questions for .NET Developer Interviews
}
[Link]($"GCD: {a}");
11. Swap two numbers without using a temporary variable.
int x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
[Link]($"x = {x}, y = {y}");
12. Check if two strings are anagrams.
string s1 = "listen", s2 = "silent";
char[] a1 = [Link]();
char[] a2 = [Link]();
[Link](a1);
[Link](a2);
[Link]([Link](a2) ? "Anagrams" : "Not Anagrams");
13. Find the missing number in an array of 1 to N.
int[] arr = {1, 2, 4, 5};
int n = 5;
int total = n * (n + 1) / 2;
int sum = [Link]();
[Link]($"Missing Number: {total - sum}");
14. Find the frequency of characters in a string.
string str = "programming";
Dictionary<char, int> freq = new Dictionary<char, int>();
foreach (char c in str)
{
if ([Link](c)) freq[c]++;
else freq[c] = 1;
}
foreach (var kv in freq)
[Link]($"{[Link]}: {[Link]}");
15. Check if a number is Armstrong.
int num = 153, sum = 0, temp = num;
while (temp > 0)
{
int digit = temp % 10;
Page 4
Top 21 C# Coding Questions for .NET Developer Interviews
sum += digit * digit * digit;
temp /= 10;
}
[Link](sum == num ? "Armstrong" : "Not Armstrong");
16. Merge two sorted arrays.
int[] a = {1, 3, 5}, b = {2, 4, 6};
int[] result = new int[[Link] + [Link]];
int i = 0, j = 0, k = 0;
while (i < [Link] && j < [Link])
{
result[k++] = a[i] < b[j] ? a[i++] : b[j++];
}
while (i < [Link]) result[k++] = a[i++];
while (j < [Link]) result[k++] = b[j++];
[Link]([Link](", ", result));
17. Left rotate an array by D positions.
int[] arr = {1, 2, 3, 4, 5};
int d = 2;
d = d % [Link];
int[] rotated = [Link](d).Concat([Link](d)).ToArray();
[Link]([Link](", ", rotated));
18. Find the first non-repeating character in a string.
string str = "swiss";
Dictionary<char, int> freq = new Dictionary<char, int>();
foreach (char c in str)
{
if ([Link](c)) freq[c]++;
else freq[c] = 1;
}
char result = [Link](x => [Link] == 1).Key;
[Link]($"First non-repeating: {result}");
19. Write a program to implement Linear Search and Binary Search.
// Linear Search
int[] arr = {1, 3, 5, 7};
int key = 5;
bool found = [Link](key);
[Link](found ? "Found" : "Not Found");
Page 5
Top 21 C# Coding Questions for .NET Developer Interviews
// Binary Search
[Link](arr);
int index = [Link](arr, key);
[Link](index >= 0 ? $"Found at {index}" : "Not Found");
20. Convert binary number to decimal and vice versa.
int binary = 1011;
int decimalNum = Convert.ToInt32([Link](), 2);
[Link]($"Decimal: {decimalNum}");
int number = 11;
string binaryStr = [Link](number, 2);
[Link]($"Binary: {binaryStr}");
21. Move all 0's to the end of array while maintaining order.
int[] arr = {0, 1, 0, 3, 12};
int index = 0;
foreach (int num in arr)
{
if (num != 0) arr[index++] = num;
}
while (index < [Link])
{
arr[index++] = 0;
}
[Link]([Link](", ", arr));
Page 6