DSA Questions in C#

DSA Questions in C#

🚀 DSA Questions with Explanation (C#)

1. Reverse a String

Question: Reverse a given string.

We convert string to array, reverse it, and convert back to string.
string str = "hello";
char[] arr = str.ToCharArray();
Array.Reverse(arr);
Console.WriteLine(new string(arr));
Output: olleh

2. Palindrome Check

Question: Check if string is same forward and backward.

Reverse the string and compare with original.
string str = "madam";
string rev = new string(str.Reverse().ToArray());

Console.WriteLine(str == rev ? "Palindrome" : "Not");
Output: Palindrome

3. Maximum in Array

Question: Find largest element.

Loop through array and track maximum value.
int[] arr = {1,5,3,9,2};
int max = arr[0];

foreach(int i in arr)
{
    if(i > max) max = i;
}

Console.WriteLine(max);
Output: 9

4. Fibonacci Series

Question: Print Fibonacci numbers.

Each number is sum of previous two numbers.
int a = 0, b = 1;

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

5. Two Sum

Question: Find indices of numbers that add to target.

Use dictionary to store values and check difference.
int[] nums = {2,7,11,15};
int target = 9;

Dictionary map = new Dictionary();

for(int i=0;i

Output: 0,1

Created by Koustubh Javheri

Keep Coding 🚀

Comments

Popular posts from this blog

DSA Easy Notes (C#)