Common Coding Interview Questions For .NET Interview

This article explains commonly asked coding example questions in technical rounds for .NET interviews. Here, I have given some examples with the code to help the developers prepare for a technical interview. It looks simple but during the interview, it will be difficult to explain or write on paper.
 
I have attached the source code with this article for all the below examples.
 

Common programs to build that interviewers ask during .NET interviews

  1. static void Main(string[] args)    
  2. {    
  3.             //Write Program to find fibonacci series for given number    
  4.             for (int i = 0; i < 10; i++)    
  5.             {    
  6.                 Console.Write("{0} ", FibbonacciSeries(i));    
  7.             }    
  8.             Console.WriteLine(Environment.NewLine);    
  9.     
  10.             Console.Write("Fibbonacci Series of {0} is : {1} ", 10, FibbonacciSeries1(10));    
  11.     
  12.             Console.WriteLine(Environment.NewLine);    
  13.     
  14.             //Write Program to calculate factorial value for given number    
  15.             Console.WriteLine("The Factorial of {0} is: {1} \n", 6, Factorial(6));    
  16.     
  17.             //Write Program to find duplicate in String    
  18.             FindDuplicateCharacterInString("CSharpCorner");    
  19.     
  20.             Console.WriteLine(Environment.NewLine);    
  21.             //Write Program to find duplicate in string array    
  22.             FindDuplicateInstringArray();    
  23.     
  24.             Console.WriteLine(Environment.NewLine);    
  25.             //Write Program to Remove duplicate in string    
  26.             RemoveDuplicate("CSharpCorner");    
  27.     
  28.             Console.WriteLine(Environment.NewLine);    
  29.             //Write Program to find number of character in string    
  30.             FindNumberofCharaterinString();    
  31.     
  32.             Console.WriteLine(Environment.NewLine);    
  33.             //Write Program to find number of words in string    
  34.             ReverseStringWords("Jignesh Kumar");    
  35.     
  36.             Console.WriteLine(Environment.NewLine);    
  37.             //Write Program to find consicutive character in string    
  38.             char[] charArray = { 'A''B''B''C''D''D''E''F''F' };    
  39.             FindConsicutiveCharacter(charArray);    
  40.     
  41.             Console.WriteLine(Environment.NewLine);    
  42.             //Write Program to check string is palindrome or not    
  43.             string[] strArray = { "WOW""NOON""ABBA""ANNA""BOB""Jimmy""Peter" };    
  44.     
  45.             foreach (var item in strArray)    
  46.             {    
  47.                 Console.WriteLine(" {0} Is palindrome {1}", item, IsStringPalindrome(item));    
  48.             }    
  49.     
  50.             Console.WriteLine(Environment.NewLine);    
  51.             //Write program to check number is palindrome or not    
  52.             int number = 121;    
  53.             Console.WriteLine("{0} is Palindrome number {1} ", number, IsNumberPalindrome(number));    
  54.     
  55.             Console.WriteLine(Environment.NewLine);    
  56.             //Write program to check number is palindrome or not    
  57.             number = 127;    
  58.             Console.WriteLine("{0} is prime number {1} ", number, IsNumberPrime(number));    
  59.             number = 128;    
  60.             Console.WriteLine("{0} is prime number {1} ", number, IsNumberPrime(number));    
  61.             Console.ReadLine();    
  62. }   

Write a program for printing a fibonacci series for a given number

  1. private static int FibbonacciSeries(int number)    
  2. {    
  3.            int firstValue = 0;    
  4.            int secondValue = 1;    
  5.            int result = 0;    
  6.            if (number == 0)    
  7.                return 0;    
  8.            if (number == 1)    
  9.                return 1;    
  10.            for (int i = 2; i <= number; i++)    
  11.            {    
  12.                result = firstValue + secondValue;    
  13.                firstValue = secondValue;    
  14.                secondValue = result;    
  15.            }    
  16.            return result;    
  17.  }    
  18.     
  19. public static string FibbonacciSeries1(int n)    
  20. {    
  21.            int a = 0, b = 1, c;    
  22.            StringBuilder sb = new StringBuilder();    
  23.            for (int i = 1; i < n; i++)    
  24.            {    
  25.                sb.Append(a.ToString() + ",");    
  26.                c = a + b;    
  27.                a = b;    
  28.                b = c;    
  29.            }    
  30.            return sb.ToString().Remove(sb.Length - 1); ;    
  31. }  

Write a program to calculate factorial value for a given number

  1. private static int Factorial(int number)    
  2. {    
  3.            int fact = 1;    
  4.            if (number == 0)    
  5.                return 0;    
  6.            if (number == 1)    
  7.                return 1;    
  8.     
  9.            for (int i = 1; i <= number; i++)    
  10.            {    
  11.                fact = fact * i;    
  12.            }    
  13.            return fact;    
  14. }   

Write a program to find duplicate in String

  1. private static void FindDuplicateCharacterInString(string inPutString)    
  2. {    
  3.     
  4.             if (string.IsNullOrEmpty(inPutString))    
  5.             {    
  6.                 Console.WriteLine("Please enter valid Input");    
  7.             }    
  8.             else    
  9.             {    
  10.                 var list = new List<char>();    
  11.                 string result = string.Empty;    
  12.     
  13.                 foreach (char item in inPutString)    
  14.                 {    
  15.                     if (list.Contains(item))    
  16.                     {    
  17.                         if (!result.Contains(item))    
  18.                             result += item;    
  19.                     }    
  20.                     else    
  21.                     {    
  22.                         list.Add(item);    
  23.                     }    
  24.                 }    
  25.                 Console.WriteLine("Duplicate Found : {0} ", result);    
  26.             }      
  27. }  

Write a program to find duplicate in a string array

  1. public static void FindDuplicateInstringArray()    
  2. {    
  3.             string[] strArray = { "Sunday""Monday""Tuesday""Wednesday""Sunday""Monday" };    
  4.             List<string> lstString = new List<string>();    
  5.             StringBuilder sb = new StringBuilder();    
  6.     
  7.             foreach (var str in strArray)    
  8.             {    
  9.                 if (lstString.Contains(str))    
  10.                 {    
  11.                     sb.Append(" " + str);    
  12.     
  13.                 }    
  14.                 else    
  15.                 {    
  16.                     lstString.Add(str);    
  17.                 }    
  18.             }    
  19.             Console.WriteLine("Duplicate Found : {0}", sb.ToString());    
  20. }   
Write a program to remove duplicate in string:
  1. public static void RemoveDuplicate(string inputString)    
  2. {    
  3.             var list = new List<char>();    
  4.     
  5.             foreach (var item in inputString)    
  6.             {    
  7.                 if (!list.Contains(item))    
  8.                 {    
  9.                     list.Add(item);    
  10.                 }    
  11.             }    
  12.     
  13.       Console.WriteLine("Orignal String {0}, After duplicate removed {1}", inputString, new string(list.ToArray()));    
  14. }   

Write a program to find a number of character in a string

  1. public static void FindNumberofCharaterinString()    
  2. {    
  3.             string StringToCount = "DotNetDeveloper";    
  4.             var result = FindOccuranceofCharacterInString(StringToCount);    
  5.     
  6.             Console.WriteLine("Number of occurrences of a character in given string");    
  7.             foreach (var count in result)    
  8.             {    
  9.                 Console.WriteLine(" {0} - {1} ", count.Key, count.Value);    
  10.             }    
  11. }    
  12.     
  13. public static SortedDictionary<charint> FindOccuranceofCharacterInString(string str)    
  14. {    
  15.  SortedDictionary<charint> count = new SortedDictionary<charint>();    
  16.     
  17.             foreach (var chr in str)    
  18.             {    
  19.                 if (!(count.ContainsKey(chr)))    
  20.                 {    
  21.                     count.Add(chr, 1);    
  22.                 }    
  23.                 else    
  24.                 {    
  25.                     count[chr]++;    
  26.                 }    
  27.             }    
  28.     
  29.     return count;    
  30. }    

Write a program to find a number of words in a string

  1. public static void ReverseStringWords(string inputString)    
  2. {    
  3.             string[] seprator = { " " };    
  4.             string[] words = inputString.Split(seprator, StringSplitOptions.RemoveEmptyEntries);    
  5.             string result = string.Empty;    
  6.             for (int i = words.Length - 1; i >= 0; i--)    
  7.             {    
  8.                 result += words[i].ToString();    
  9.             }    
  10.      Console.WriteLine("Reverse words in string {0}", result);    
  11. }    

Write a program to find the consecutive characters in a string

  1. public static void FindConsicutiveCharacter(char[] characterArray)    
  2. {    
  3.            int len = characterArray.Length - 1;    
  4.            List<char> result = new List<char>();    
  5.            for (int i = 0; i < len; i++)    
  6.            {    
  7.                for (int j = i + 1; j <= len; j++)    
  8.                {    
  9.                    if (characterArray[i] == characterArray[j])    
  10.                    {    
  11.                        if (!result.Contains(characterArray[i]))    
  12.                            result.Add(characterArray[i]);    
  13.                        continue;    
  14.                    }    
  15.                    else    
  16.                    {    
  17.                        break;    
  18.                    }    
  19.                }    
  20.            }    
  21.     
  22.            Console.WriteLine("Consicutive Character found {0} "new string(result.ToArray()));    
  23. }    

Write a program to check if a string/number is a palindrome or not

  1. public static bool IsStringPalindrome(string inputString)    
  2. {    
  3.             int minIdex = 0;    
  4.             int maxIdex = inputString.Length - 1;    
  5.             while (true)    
  6.             {    
  7.                 if (minIdex > maxIdex)    
  8.                 {    
  9.                     return true;    
  10.                 }    
  11.                 char charfromLeft = inputString[minIdex];    
  12.                 char charfromRight = inputString[maxIdex];    
  13.                 if (charfromLeft != charfromRight)    
  14.                 {    
  15.                     return false;    
  16.                 }    
  17.                 minIdex++;    
  18.                 maxIdex--;    
  19.             }      
  20. }      
  21. public static bool IsNumberPalindrome(int number)    
  22. {    
  23.             int reminder, sum = 0;    
  24.             int tempNumber;    
  25.             tempNumber = number;    
  26.             bool IsPalindrome = false;    
  27.             while (number > 0)    
  28.             {    
  29.                 reminder = number % 10;    
  30.                 number = number / 10;    
  31.                 sum = sum * 10 + reminder;    
  32.                 if (tempNumber == sum)    
  33.                 {    
  34.                     IsPalindrome = true;    
  35.                 }    
  36.             }    
  37.             return IsPalindrome;    
  38. }    

Write a program to check if a number is prime or not

  1. public static bool IsNumberPrime(int number)    
  2. {    
  3.             int i;    
  4.             for (i = 2; i <= number - 1; i++)    
  5.             {    
  6.                 if (number % i == 0)    
  7.                 {    
  8.                     return false;    
  9.                 }    
  10.             }    
  11.             if (i == number)    
  12.             {    
  13.                 return true;    
  14.             }    
  15.             return false;    
  16. }   
 
Please find my other articles if you wish to read more about .NET and other cutting-edge technologies.
  • Swagger UI Integration With Web API For Testing And Documentation Click Here 
  • Tricks and Tips In Visual Studio To Speed Up Your Code Using Default Code Snippet Feature Click Here
  • Create Documentation With Sandcastle Help Builder Click Here
  • Test Web API using SoapUI Click Here
  • Getting Started with TypeScript Click Here
  • LINQ extension methods Click Here

Recommended Ebook

Diving Into Microsoft .NET Entity Framework

Download Now!
Similar Articles