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
- static void Main(string[] args)
- {
-
- for (int i = 0; i < 10; i++)
- {
- Console.Write("{0} ", FibbonacciSeries(i));
- }
- Console.WriteLine(Environment.NewLine);
-
- Console.Write("Fibbonacci Series of {0} is : {1} ", 10, FibbonacciSeries1(10));
-
- Console.WriteLine(Environment.NewLine);
-
-
- Console.WriteLine("The Factorial of {0} is: {1} \n", 6, Factorial(6));
-
-
- FindDuplicateCharacterInString("CSharpCorner");
-
- Console.WriteLine(Environment.NewLine);
-
- FindDuplicateInstringArray();
-
- Console.WriteLine(Environment.NewLine);
-
- RemoveDuplicate("CSharpCorner");
-
- Console.WriteLine(Environment.NewLine);
-
- FindNumberofCharaterinString();
-
- Console.WriteLine(Environment.NewLine);
-
- ReverseStringWords("Jignesh Kumar");
-
- Console.WriteLine(Environment.NewLine);
-
- char[] charArray = { 'A', 'B', 'B', 'C', 'D', 'D', 'E', 'F', 'F' };
- FindConsicutiveCharacter(charArray);
-
- Console.WriteLine(Environment.NewLine);
-
- string[] strArray = { "WOW", "NOON", "ABBA", "ANNA", "BOB", "Jimmy", "Peter" };
-
- foreach (var item in strArray)
- {
- Console.WriteLine(" {0} Is palindrome {1}", item, IsStringPalindrome(item));
- }
-
- Console.WriteLine(Environment.NewLine);
-
- int number = 121;
- Console.WriteLine("{0} is Palindrome number {1} ", number, IsNumberPalindrome(number));
-
- Console.WriteLine(Environment.NewLine);
-
- number = 127;
- Console.WriteLine("{0} is prime number {1} ", number, IsNumberPrime(number));
- number = 128;
- Console.WriteLine("{0} is prime number {1} ", number, IsNumberPrime(number));
- Console.ReadLine();
- }
Write a program for printing a fibonacci series for a given number
- private static int FibbonacciSeries(int number)
- {
- int firstValue = 0;
- int secondValue = 1;
- int result = 0;
- if (number == 0)
- return 0;
- if (number == 1)
- return 1;
- for (int i = 2; i <= number; i++)
- {
- result = firstValue + secondValue;
- firstValue = secondValue;
- secondValue = result;
- }
- return result;
- }
-
- public static string FibbonacciSeries1(int n)
- {
- int a = 0, b = 1, c;
- StringBuilder sb = new StringBuilder();
- for (int i = 1; i < n; i++)
- {
- sb.Append(a.ToString() + ",");
- c = a + b;
- a = b;
- b = c;
- }
- return sb.ToString().Remove(sb.Length - 1); ;
- }
Write a program to calculate factorial value for a given number
- private static int Factorial(int number)
- {
- int fact = 1;
- if (number == 0)
- return 0;
- if (number == 1)
- return 1;
-
- for (int i = 1; i <= number; i++)
- {
- fact = fact * i;
- }
- return fact;
- }
Write a program to find duplicate in String
- private static void FindDuplicateCharacterInString(string inPutString)
- {
-
- if (string.IsNullOrEmpty(inPutString))
- {
- Console.WriteLine("Please enter valid Input");
- }
- else
- {
- var list = new List<char>();
- string result = string.Empty;
-
- foreach (char item in inPutString)
- {
- if (list.Contains(item))
- {
- if (!result.Contains(item))
- result += item;
- }
- else
- {
- list.Add(item);
- }
- }
- Console.WriteLine("Duplicate Found : {0} ", result);
- }
- }
Write a program to find duplicate in a string array
- public static void FindDuplicateInstringArray()
- {
- string[] strArray = { "Sunday", "Monday", "Tuesday", "Wednesday", "Sunday", "Monday" };
- List<string> lstString = new List<string>();
- StringBuilder sb = new StringBuilder();
-
- foreach (var str in strArray)
- {
- if (lstString.Contains(str))
- {
- sb.Append(" " + str);
-
- }
- else
- {
- lstString.Add(str);
- }
- }
- Console.WriteLine("Duplicate Found : {0}", sb.ToString());
- }
Write a program to remove duplicate in string:
- public static void RemoveDuplicate(string inputString)
- {
- var list = new List<char>();
-
- foreach (var item in inputString)
- {
- if (!list.Contains(item))
- {
- list.Add(item);
- }
- }
-
- Console.WriteLine("Orignal String {0}, After duplicate removed {1}", inputString, new string(list.ToArray()));
- }
Write a program to find a number of character in a string
- public static void FindNumberofCharaterinString()
- {
- string StringToCount = "DotNetDeveloper";
- var result = FindOccuranceofCharacterInString(StringToCount);
-
- Console.WriteLine("Number of occurrences of a character in given string");
- foreach (var count in result)
- {
- Console.WriteLine(" {0} - {1} ", count.Key, count.Value);
- }
- }
-
- public static SortedDictionary<char, int> FindOccuranceofCharacterInString(string str)
- {
- SortedDictionary<char, int> count = new SortedDictionary<char, int>();
-
- foreach (var chr in str)
- {
- if (!(count.ContainsKey(chr)))
- {
- count.Add(chr, 1);
- }
- else
- {
- count[chr]++;
- }
- }
-
- return count;
- }
Write a program to find a number of words in a string
- public static void ReverseStringWords(string inputString)
- {
- string[] seprator = { " " };
- string[] words = inputString.Split(seprator, StringSplitOptions.RemoveEmptyEntries);
- string result = string.Empty;
- for (int i = words.Length - 1; i >= 0; i--)
- {
- result += words[i].ToString();
- }
- Console.WriteLine("Reverse words in string {0}", result);
- }
Write a program to find the consecutive characters in a string
- public static void FindConsicutiveCharacter(char[] characterArray)
- {
- int len = characterArray.Length - 1;
- List<char> result = new List<char>();
- for (int i = 0; i < len; i++)
- {
- for (int j = i + 1; j <= len; j++)
- {
- if (characterArray[i] == characterArray[j])
- {
- if (!result.Contains(characterArray[i]))
- result.Add(characterArray[i]);
- continue;
- }
- else
- {
- break;
- }
- }
- }
-
- Console.WriteLine("Consicutive Character found {0} ", new string(result.ToArray()));
- }
Write a program to check if a string/number is a palindrome or not
- public static bool IsStringPalindrome(string inputString)
- {
- int minIdex = 0;
- int maxIdex = inputString.Length - 1;
- while (true)
- {
- if (minIdex > maxIdex)
- {
- return true;
- }
- char charfromLeft = inputString[minIdex];
- char charfromRight = inputString[maxIdex];
- if (charfromLeft != charfromRight)
- {
- return false;
- }
- minIdex++;
- maxIdex--;
- }
- }
- public static bool IsNumberPalindrome(int number)
- {
- int reminder, sum = 0;
- int tempNumber;
- tempNumber = number;
- bool IsPalindrome = false;
- while (number > 0)
- {
- reminder = number % 10;
- number = number / 10;
- sum = sum * 10 + reminder;
- if (tempNumber == sum)
- {
- IsPalindrome = true;
- }
- }
- return IsPalindrome;
- }
Write a program to check if a number is prime or not
- public static bool IsNumberPrime(int number)
- {
- int i;
- for (i = 2; i <= number - 1; i++)
- {
- if (number % i == 0)
- {
- return false;
- }
- }
- if (i == number)
- {
- return true;
- }
- return false;
- }
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