Logical Interview Questions


https://codingsight.com/tricky-questions-about-c-sharp/

Remove duplicate characters from String

 string myStr = "kkllmmnnoo";
 Console.WriteLine("Initial String: "+myStr);
 var unique = new HashSet<char>(myStr);
 Console.Write("New String after removing duplicates: ");
 foreach (char c in unique)
 Console.Write(c);

Find duplicate characters in a string

            StringBuilder result = new StringBuilder();  
            StringBuilder duplicateChar = new StringBuilder();  
  
            foreach (var item in _title)  
            {  
                if (result.ToString().IndexOf(item.ToString().ToLower()) == -1)  
                {  
                    result.Append(item);  
                }  
                else  
                {  
                    duplicateChar.Append(item);  
                }  
            }  
            return duplicateChar;  
        }  

Reverse a string.

  1.   for (int i = _title.Length – 1; i >= 0; i–)  
  2.             {  
  3.                 result += _title[i];  
  4.             }  
  5.   
  6.             return result; 

Check if a string is a palindrome or not.

  1.  var min = 0;  
  2.             var max = _title.Length – 1;  
  3.   
  4.             while (max >= 0)  
  5.             {  
  6.                 if (_title[min] == _title[max])  
  7.                 {  
  8.                     min++;  
  9.                     max–;  
  10.                 }  
  11.                 else  
  12.                 {  
  13.                     return false;  
  14.                 }  
  15.             }  
  16.   
  17.             return result;  

Check max occurrence of a character in the string

  1.  public char? CheckMaxOccuranceOfChar(string _title)  
  2.         {  
  3.             char? maxOccuranceChar = null;  
  4.             int maxOccuranceValue = 0;  
  5.   
  6.             if (string.IsNullOrEmpty(_title))  
  7.                 return null;  
  8.   
  9.             _title = _title.ToLower().Trim();  
  10.             char[] arr = _title.ToCharArray();  
  11.   
  12.             Dictionary<charint> _dictionary = new Dictionary<charint>();  
  13.             for (int i = 0; i < arr.Length; i++)  
  14.             {  
  15.                 if (arr[i] != ‘ ‘)  
  16.                 {  
  17.                     if (!_dictionary.ContainsKey(arr[i]))  
  18.                     {  
  19.                         _dictionary.Add(arr[i], 1);  
  20.                     }  
  21.                     else  
  22.                     {  
  23.                         _dictionary[arr[i]]++;  
  24.                     }  
  25.                 }  
  26.             }  
  27.   
  28.   
  29.             foreach (KeyValuePair<charint> item in _dictionary)  
  30.             {  
  31.                 if (item.Value > maxOccuranceValue)  
  32.                 {  
  33.                     maxOccuranceChar = item.Key;  
  34.                     maxOccuranceValue = item.Value;  
  35.                 }  
  36.             }  
  37.   
  38.             return maxOccuranceChar;  
  39.         }  
static char getMaxOccuringChar(String str) 
    { 
        // Create array to keep the count of 
        // individual characters and  
        // initialize the array as 0 
        int []count = new int[ASCII_SIZE]; 
      
        // Construct character count array 
        // from the input string. 
        int len = str.Length; 
        for (int i = 0; i < len; i++) 
            count[str[i]]++; 
      
        int max = -1; // Initialize max count 
        char result = ' '; // Initialize result 
      
        // Traversing through the string and  
        // maintaining the count of each character 
        for (int i = 0; i < len; i++) { 
            if (max < count[str[i]]) { 
                max = count[str[i]]; 
                result = str[i]; 
            } 
        } 
      
        return result; 
    } 
      

Exchange Two integer variable value without using third variable.

  1.  a = a + b; // Now a would be 15   
  2.     b = a – b; // Now b would be 15-5=10 ……..   
  3.     a = a – b; // Now a would be 15-10=5……..

Swap neighbor char in string, For Example string “TAPAN” would be:

neighbor char in string
  1. public static string SwapNeighbourChar(string strToSwap)  
  2. {  
  3.     char[] arraStr = strToSwap.ToCharArray();  
  4.     StringBuilder strbuild = new StringBuilder();  
  5.     for (int i = 0; i <= arraStr.Length – 1; i++)  
  6.     {  
  7.         if (i != arraStr.Length – 1)  
  8.         {  
  9.             strbuild.Append(arraStr[i + 1]);  
  10.         }  
  11.         strbuild.Append(arraStr[i]);  
  12.         i = i + 1;  
  13.     }  
  14.     return strbuild.ToString();  
  15. }  

Is prime number?

  1. public static bool IsPrimeNumbers(int number)  
  2. {  
  3.     bool returnMsg = false;  
  4.     for (int i = 2; i <= number; i++)  
  5.     {  
  6.         if ((number % i) == 0 && number != i)  
  7.         {  
  8.             returnMsg = false;  
  9.             return returnMsg;  
  10.         }  
  11.         else if (number == i)  
  12.         {  
  13.             returnMsg = true;  
  14.             return returnMsg;  
  15.         }  
  16.     }  
  17.     return returnMsg;  
  18. }
  1. public static void PrintFibonacciSeries(int limit)  
  2. {  
  3.     int digit1, digit2, digit3;  
  4.     digit1 = 0;  
  5.     digit2 = 1;  
  6.     digit3 = digit1 + digit2;  
  7.     Console.WriteLine(digit1);  
  8.     Console.WriteLine(digit2);  
  9.     Console.WriteLine(digit3);  
  10.     for (int i = 0; i < limit; i++)  
  11.     {  
  12.         digit1 = digit2;  
  13.         digit2 = digit3;  
  14.         digit3 = digit1 + digit2;  
  15.         Console.WriteLine(digit3);  
  16.     }  
  17. }

Ascending Order

 for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[i] > arr1[j])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }

 array the sorted using a single loop?

 for (int j = 0; j < arr.Length - 1; j++) 
        { 
  
            // Checking the condition for two 
            // simultaneous elements of the array 
            if (arr[j] > arr[j + 1]) 
            { 
  
                // Swapping the elements. 
                int temp = arr[j]; 
                arr[j] = arr[j + 1]; 
                arr[j + 1] = temp; 
  
                // updating the value of j = -1 
                // so after getting updated for j++ 
                // in the loop it becomes 0 and 
                // the loop begins from the start. 
                j = -1; 
            } 
        } 
        return arr; 
    } 

Reverse number

  1. int  n, reverse=0, rem;           
  2.        Console.Write(“Enter a number: “);      
  3.        n= int.Parse(Console.ReadLine());     
  4.        while(n!=0)      
  5.        {      
  6.         rem=n%10;        
  7.         reverse=reverse*10+rem;      
  8.         n/=10;      
  9.        }      
  10.        Console.Write(“Reversed Number: “+reverse);       

Find the second maximum number in an array with the smallest complexity

int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
int largest = int.MinValue;
int second = int.MinValue;
foreach (int i in myArray)
{
 if (i > largest)
 {
  second = largest;
  largest = i;
 }
else if (i > second)
    second = i;
}

System.Console.WriteLine(second);

Try this (using LINQ):

int secondHighest = (from number in test
                             orderby number descending
                             select number).Distinct().Skip(1).First()