Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

C# Coding Questions For Technical Interviews - Part-1

1. How to reverse a string?



using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string to reverse: ");
        string input = Console.ReadLine();

        string reversed = ReverseString(input);
        Console.WriteLine($"Reversed string: {reversed}");
    }

    static string ReverseString(string str)
    {
        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
}

    

Explanation:

  1. ReverseString Method:

    • Converts the string into a character array.
    • Uses Array.Reverse to reverse the array.
    • Creates a new string from the reversed character array.
  2. Input and Output:

    • Takes input from the user.
    • Outputs the reversed string.

2. How to find if the given string is a palindrome or not?



using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();

        if (IsPalindrome(input))
            Console.WriteLine("The string is a palindrome.");
        else
            Console.WriteLine("The string is not a palindrome.");
    }

    static bool IsPalindrome(string str)
    {
        string reversed = ReverseString(str);
        return string.Equals(str, reversed, StringComparison.OrdinalIgnoreCase);
    }

    static string ReverseString(string str)
    {
        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
}

    

Explanation:

  1. IsPalindrome Method:

    • Reverses the input string.
    • Compares the original string with the reversed one using StringComparison.OrdinalIgnoreCase for case-insensitive comparison.
  2. ReverseString Method:

    • Reverses the input string using a char[] array and Array.Reverse.
  3. Input and Output:

    • Takes input from the user.
    • Outputs whether the string is a palindrome or not.

3. How to reverse the order of words in a given string?



using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();

        string reversedWords = ReverseWords(input);
        Console.WriteLine($"Reversed words: {reversedWords}");
    }

    static string ReverseWords(string str)
    {
        string[] words = str.Split(' ');
        Array.Reverse(words);
        return string.Join(" ", words);
    }
}


    

Explanation:

  1. ReverseWords Method:

    • Splits the string into an array of words using Split(' ').
    • Reverses the order of the words using Array.Reverse.
    • Joins the reversed array back into a single string using string.Join.
  2. Input and Output:

    • Takes a string from the user.
    • Outputs the string with the order of words reversed.

4. How to recount the occurrence of each character in a string?



using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();

        Dictionary <char, int> charCount = CountCharacters(input);

        Console.WriteLine("Character occurrences:");
        foreach (var pair in charCount)
        {
            Console.WriteLine($"'{pair.Key}': {pair.Value}");
        }
    }

    static Dictionary <char, int> CountCharacters(string str)
    {
        var result = new Dictionary<char, int>();

        foreach (char c in str)
        {
            if (result.ContainsKey(c))
                result[c]++;
            else
                result[c] = 1;
        }

        return result;
    }
}


    

Explanation:

  1. CountCharacters Method:

    • Loops through each character in the string.
    • Uses a Dictionary<char, int> to store character counts.
    • If a character already exists in the dictionary, its count is incremented. Otherwise, it's added with a count of 1.
  2. Input and Output:

    • Takes a string input from the user.
    • Displays each character and its corresponding count.

5. How to remove duplicate characters from a string?



using System;
using System.Linq;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string: ");
        string input = Console.ReadLine();

        string result = RemoveDuplicates(input);
        Console.WriteLine($"String without duplicates: {result}");
    }

    static string RemoveDuplicates(string str)
    {
        return new string(str.Distinct().ToArray());
    }
}


    

Explanation:

  1. Distinct Method:

    • The Distinct method from System.Linq removes duplicate characters from the string.
  2. Conversion:

    • str.Distinct() returns an enumerable collection of unique characters.
    • ToArray() converts it back to a character array, and new string() forms the result string.
  3. Input and Output:

    • Takes a string from the user.
    • Outputs the string with duplicates removed.

6.How to find all possible substring of a given string?



using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a string or number: ");
        string input = Console.ReadLine();

        Console.WriteLine("All possible substrings:");
        FindSubstrings(input);
    }

    static void FindSubstrings(string str)
    {
        for (int i = 0; i < str.Length; i++)
        {
            for (int j = i + 1; j <= str.Length; j++)
            {
                Console.WriteLine(str.Substring(i, j - i));
            }
        }
    }
}


    

Explanation:

  1. Outer Loop:

    • Iterates through each starting index i of the substring.
  2. Inner Loop:

    • Iterates through the ending index j, starting from i + 1.
  3. Substring Method:

    • Extracts a substring from index i with length (j - i).
  4. Input and Output:

    • Takes a string or number as input.
    • Outputs all possible substrings.

For example:

Input: abc

Output:

a
ab
abc
b
bc
c

Post a Comment

0 Comments