20 Most Asked C# Coding Problems (With Solutions)

โญ Introduction

These problems appear again and again in:

  • C# interviews
  • Online coding rounds
  • Machine coding rounds
  • Technical screenings

If you can solve these confidently, you are 90% interview-ready.


๐Ÿ”น 1. Reverse a String

Checks: String handling, loops

string input = "hello";
char[] arr = input.ToCharArray();
Array.Reverse(arr);
Console.WriteLine(new string(arr));


๐Ÿ”น 2. Check Palindrome

Checks: Logic, string/number manipulation

string s = "madam";
string rev = new string(s.Reverse().ToArray());
Console.WriteLine(s == rev);


๐Ÿ”น 3. Find Factorial

Checks: Recursion / loops

int Factorial(int n)
{
    return n <= 1 ? 1 : n * Factorial(n - 1);
}


๐Ÿ”น 4. Fibonacci Series

Checks: Loops, logic

int a = 0, b = 1;
for (int i = 0; i < 5; i++)
{
    Console.Write(a + " ");
    int temp = a + b;
    a = b;
    b = temp;
}


๐Ÿ”น 5. Check Prime Number

Checks: Math logic

bool IsPrime(int n)
{
    if (n <= 1) return false;
    for (int i = 2; i <= Math.Sqrt(n); i++)
        if (n % i == 0) return false;
    return true;
}


๐Ÿ”น 6. Find Largest Number in Array

Checks: Arrays

int[] arr = { 3, 7, 2 };
Console.WriteLine(arr.Max());


๐Ÿ”น 7. Remove Duplicate Characters

Checks: Collections, LINQ

string s = "programming";
string result = new string(s.Distinct().ToArray());
Console.WriteLine(result);


๐Ÿ”น 8. Reverse Words in a Sentence

Checks: String split

string s = "I love CSharp";
var words = s.Split(' ').Reverse();
Console.WriteLine(string.Join(" ", words));


๐Ÿ”น 9. Count Character Occurrences

Checks: Dictionary

string s = "hello";
var dict = s.GroupBy(c => c)
            .ToDictionary(g => g.Key, g => g.Count());


๐Ÿ”น 10. Swap Two Numbers (Without Temp)

Checks: Logic

int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;


๐Ÿ”น 11. Find Second Largest Number

Checks: Sorting, logic

int[] arr = { 5, 1, 9, 7 };
int secondLargest = arr.Distinct()
                        .OrderByDescending(x => x)
                        .Skip(1)
                        .First();


๐Ÿ”น 12. Check Armstrong Number

Checks: Math + loops

int n = 153, sum = 0, temp = n;
while (temp > 0)
{
    int d = temp % 10;
    sum += d * d * d;
    temp /= 10;
}
Console.WriteLine(sum == n);


๐Ÿ”น 13. Find Missing Number in Array

Checks: Math logic

int[] arr = { 1, 2, 4, 5 };
int n = 5;
int expected = n * (n + 1) / 2;
Console.WriteLine(expected - arr.Sum());


๐Ÿ”น 14. Find Duplicate Elements

Checks: LINQ

int[] arr = { 1, 2, 3, 2, 4 };
var duplicates = arr.GroupBy(x => x)
                    .Where(g => g.Count() > 1)
                    .Select(g => g.Key);


๐Ÿ”น 15. Sort Array Without Built-in Sort

Checks: Algorithm basics

int[] arr = { 3, 1, 2 };
for (int i = 0; i < arr.Length; i++)
    for (int j = i + 1; j < arr.Length; j++)
        if (arr[i] > arr[j])
            (arr[i], arr[j]) = (arr[j], arr[i]);


๐Ÿ”น 16. Count Vowels in String

Checks: String traversal

string s = "education";
int count = s.Count(c => "aeiou".Contains(c));


๐Ÿ”น 17. Find Sum of Digits

Checks: Math logic

int n = 123, sum = 0;
while (n > 0)
{
    sum += n % 10;
    n /= 10;
}


๐Ÿ”น 18. Check Anagram

Checks: String comparison

string s1 = "listen";
string s2 = "silent";
Console.WriteLine(
    new string(s1.OrderBy(c => c).ToArray()) ==
    new string(s2.OrderBy(c => c).ToArray())
);


๐Ÿ”น 19. Find Common Elements in Two Arrays

Checks: LINQ, sets

int[] a = { 1, 2, 3 };
int[] b = { 2, 3, 4 };
var common = a.Intersect(b);


๐Ÿ”น 20. Reverse an Integer

Checks: Number manipulation

int n = 123, rev = 0;
while (n > 0)
{
    rev = rev * 10 + n % 10;
    n /= 10;
}
Console.WriteLine(rev);


๐Ÿง  What Interviewers Really Look For

โœ” Clean logic
โœ” Edge case handling
โœ” Readable code
โœ” Correct use of C# features
โœ” Ability to explain solution


๐ŸŽฏ Interview Tip

After writing code, always explain:

  • Time complexity
  • Edge cases
  • Alternative approach

This impresses interviewers more than fast typing.


๐ŸŽ‰ Conclusion

These 20 C# coding problems cover:
โœ” Strings
โœ” Arrays
โœ” Numbers
โœ” LINQ
โœ” Logic building

Master these and you will confidently clear:

  • C# interviews
  • Coding rounds
  • Machine coding tests

Leave a Comment