Skip to content

adi501/C_Sharp_Interview_Logical_Questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Find second largest number in array in c# ?

int[] arr = {1, 2, 3,9,10, 4, 5};
Array.Sort(arr);
int a = 0, b = 0;
for (int i = 0; i < arr.Length; i++)
{
    if (a < arr[i])
        a = arr[i];
}
for (int i = 0; i < arr.Length; i++)
{
    if (b<arr[i] && a!=arr[i])
        b=arr[i];
}
Console.WriteLine("Second Largest Number: "+b);
Console.ReadLine();

image

Find missing number in array in c# ?

int[] arr = { 1, 2, 9, 10, 4, 5 };
Array.Sort(arr);
int a = arr[0], b = arr[arr.Length - 1];
bool status=false;
for(int i = a; i <=b; i++)
{
    for(int j = 0; j < arr.Length; j++)
    {
        if (i == arr[j])
        {
            status = true;
            break;
        }
        else
        {
            status = false;
        }
    }
    if (status==false)
    {
        Console.WriteLine(i);
    }
}
Console.ReadLine();

image

Factorial program


int i = 5;
int result=1; //(5*4*3*2*1)
for (int j = 1; j <=5; j++)
{
   result *= j;
}
Console.WriteLine("Factorial of " + i + " is :" + result);
Console.ReadLine();

image

Fibonacci Series Example

//Fibonacci_Series_Example
//0,1,1,2,3,5,8,13,21,34 (upto 50)

int firstNum = 0, secondNumber = 1;
Console.WriteLine(firstNum);
Console.WriteLine(secondNumber);
int result=firstNum+secondNumber;
while(result<50)
{
    Console.WriteLine(result);
    firstNum = secondNumber;
    secondNumber = result;
    result = firstNum + secondNumber;
}
Console.ReadLine();

image

Prime Number Example

//Prime_number_Exp
//2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 (1 to 50)
bool isPrime = true;
int i, j;
Console.WriteLine("Prime Numbers are : ");
for (i = 2; i <= 50; i++)
{
    for (j = 2; j <= 50; j++)
    {
        if (i != j && i % j == 0)
        {
            isPrime = false;
            break;
        }
    }
    if (isPrime)
    {
        Console.Write(i+",");
    }
    isPrime = true;
}
Console.ReadLine();

image

Biggest Number in Array?

//biggest number in array

int[] arr= {1,2,3,9,5,6,7};
int bigNmuber = arr[0];
for (int i = 0; i < arr.Length; i++)
{
    if(arr[i] > bigNmuber)
    {
        bigNmuber = arr[i];
    }
}
Console.WriteLine("Biggest Number in given Array: " + bigNmuber);
Console.ReadLine();

image

Find first non-repeating element in a given Array

Console.WriteLine(findNonRepeating([1, 2, 1, 3, 4, 3, 4]));
Console.ReadLine();
 int findNonRepeating(int[] input)
{
    for(int i = 0;i<input.Length;i++)
    {
        int j;
        for ( j= 0; j < input.Length; j++)
        {
            if (i!=j && input[i] == input[j])
            {
                break;
            }
        }
        if (j == input.Length)
            return input[i];
    }
    return -1;
}

image

image

Reverse Integer in c#

// input: 12345 // output: 54321
int input = 12345;
int output = 0;
while (input != 0)
{
    int remainder = input % 10;
    output = output * 10 + remainder;
    input = input / 10;
}
Console.WriteLine(output);  //54321
Console.ReadLine();

Fibonacci Series in c#

//Fibonacci_Series_Example
//0,1,1,2,3,5,8,13,21,34 (upto 50)

int firstNum = 0, secondNumber = 1;
Console.WriteLine(firstNum);
Console.WriteLine(secondNumber);
int result=firstNum+secondNumber;
while(result<50)
{
    Console.WriteLine(result);
    firstNum = secondNumber;
    secondNumber = result;
    result = firstNum + secondNumber;
}
Console.ReadLine();


Threading Interview Question (Output for below code?)

//public static void Demo()
//{
    var task1 = StartSchool();
    var task2 = TeachClass1();
    var task3 = TeachClass2();
    Task.WaitAll(task1, task2, task3);

Console.ReadLine();
//}

 static async Task StartSchool()
{
    await Task.Run(() =>
    {
        Thread.Sleep(8000);
        Console.WriteLine("School Started");
    });
}
 static async Task TeachClass1()
{
    await Task.Run(() =>
    {
        Thread.Sleep(2000);
        Console.WriteLine("Taught class 1");
    });
}
 static async Task TeachClass2()
{
    await Task.Run(() =>
    {
        Thread.Sleep(3000);
        Console.WriteLine("Taught class 2");
    });

}

image


Find the longest substring without repeating characters


//Input: "abcabcbb" find the longest substring without repeating characters.
//Output: The longest substring is "abc".

string input = "abcabcbb";
List<string> substrings = new List<string>();
for (int i = 0; i < input.Length; i++)
{
    string substring = "";
    for (int j = i; j < input.Length; j++)
    {
        if (substring.Contains(input[j]))
        {
            break;
        }
        substring += input[j];
    }
    substrings.Add(substring);

}
var a = substrings.OrderByDescending(x => x.Length).FirstOrDefault();
Console.WriteLine($"The longest substring is \"{a}\".");
Console.ReadLine();

image


Reverse an array without using Array.Reverse()

int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
for (int i = 0; i < arr.Length / 2; i++)
{
    //Methods 1
    //int tmp = arr[i];
    //arr[i] = arr[arr.Length - i - 1];
    //arr[arr.Length - i - 1] = tmp;

    //Methods 2
    //Note : assume a=arr[i] and b=arr[arr.Length - i - 1] , then a=a+b, b=a-b, a=a-b
    arr[i] = arr[i] + arr[arr.Length - i - 1]; //a=a+b
    arr[arr.Length - i - 1] = arr[i] - arr[arr.Length - i - 1]; //b=a-b
    arr[i] = arr[i] - arr[arr.Length - i - 1]; //a=a-b
}
Console.WriteLine(string.Join(",", arr));

image