Most Important Fundamental of Programming Interview Questions and Answers for Fresh Graduates

MUHAMMAD WAHEED
10 min readDec 3, 2022

Q1: Write a program to find duplicate elements in an Array.

int arr = {1,2,5,5,6,6,7,8,9};

for(int i = 0 ; i<arr.length; i++)
{
for(int j = i+1; j<arr.length; j++)
{
if(arr[i] == arr[j])
{
Console.WriteLine("Duplicate element in Array is " +arr[i]);
}
}

}



Algorithm to Find Duplicate Elements in an Array:

1. Initialize an array arr = {1, 2, 5, 5, 6, 6, 7, 8, 9}.
2. Use a nested loop with an outer loop (i) and an inner loop (j).
3. Outer Loop (i):
a. Iterate i from 0 to arr.Length - 1.
b. Inner Loop (j):
i. Iterate j from i + 1 to arr.Length - 1.
ii. Check if arr[i] is equal to arr[j].
a. If true, print "Duplicate element in Array is " + arr[i].
4. End.

Note: This algorithm compares each element with every other element in the array to find duplicates.
It has a time complexity of O(n^2), where n is the length of the array.
For larger arrays, a HashSet-based approach is often more efficient.

Q2: Write a program to find duplicate elements in Strings.

string str = "asdjghjjf";

char[] ch = str.ToCharArray();

for(int i = 0; i<ch.length;i++)
{
for(int j = i+1; j<ch.length; j++)
{
if(ch[i]==ch[j])
{
Console.WriteLine("Duplicate Element of String is " +ch[i])
}
}
}



Algorithm to Find Duplicate Characters in a String:

1. Initialize a string str = "asdjghjjf".
2. Convert the string to a character array ch.
3. Use a nested loop with an outer loop (i) and an inner loop (j).
4. Outer Loop (i):
a. Iterate i from 0 to ch.Length - 1.
b. Inner Loop (j):
i. Iterate j from i + 1 to ch.Length - 1.
ii. Check if ch[i] is equal to ch[j].
a. If true, print "Duplicate Element of String is " + ch[i].
5. End.

Note: This algorithm compares each character with every other character in the string to find duplicates.
It has a time complexity of O(n^2), where n is the length of the string.
For larger strings, a HashSet-based approach is often more efficient.

Q3: Write a program to Combine two arrays without duplicate elements.

string[] animals = {"Cow","Monkey","Birds","Pig"} 
string[] birds = {"Parrot","Monkey","Birds","Key"}

var all = animals.union(birds).toArray();
Array.ForEach(all, s=>console.writeline(s));

Q4: Write a program to Remove All duplicate Elements in an array.

string[] animals = {"Cow","Monkey","Birds","Pig","Cow","Pig"} 
string[] dist = animals.Distinct().toArray();
Array.ForEach(dist, s=>console.writeline(s))

Algorithm to Print Distinct Elements from a String Array:

1. Initialize a string array animals = {"Cow", "Monkey", "Birds", "Pig", "Cow", "Pig"}.
2. Use the Distinct() method to create a new array dist with unique elements from animals.
3. Use the toArray() method to convert the IEnumerable result of Distinct() to an array.
4. Iterate through each element in the dist array using Array.ForEach.
a. For each element, print it to the console using Console.WriteLine.
5. End.

Note: The purpose of this algorithm is to print the distinct elements from the original array animals.

Q5: Write a program to Remove duplicate elements in a string Array.

        int i = 0, j = 0;
string[] arr1 = new string[]{ "cat", "cat", "dog", "cat", "donkey"};
for (i = 0; i < arr1.Length; i++)
{
for (j = 0; j < arr1.Length; j++)
{
if (i == j)
continue;
if (arr1[j] == arr1[i])
break;
}
if (arr1.Length == j)
{
Console.Write (arr1[i] + " ");
}
}

Q6: Write a program to find the Number Armstrong.

            int num, temp, sum = 0, rem;
Console.WriteLine("Please Enter Integer");
num = Convert.ToInt32(Console.ReadLine());
temp = num;
while (num > 0)
{
rem = num % 10;
sum = sum + rem * rem * rem;
num = num / 10;
}
if (sum == temp)
{
Console.WriteLine("The Number are Armstrong");
}
else
{
Console.WriteLine("The Number are not Armstrong");

}


Algorithm to Check if a Number is Armstrong:

1. Initialize variables: num, temp, sum = 0, rem.
2. Print the message "Please Enter Integer" to prompt the user.
3. Read an integer from the user and store it in the variable num.
4. Copy the value of num to the variable temp for later comparison.
5. Start a while loop with the condition num > 0:
a. Calculate the remainder rem by taking num % 10.
b. Calculate the sum by adding rem^3 to the existing sum.
c. Update num by performing integer division (num / 10).
6. After the loop, check if the sum is equal to the original number (temp).
a. If true, print "The Number is Armstrong."
b. If false, print "The Number is not Armstrong."
7. End.

Note: Armstrong numbers are numbers that are equal to the sum of their own digits raised to the power of the number of digits in the number.

Q7: Write a program to find the Factorial Number.

            int num, fact = 1;
Console.WriteLine("Enter Integer");
num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <=num; i++)
{
fact = fact * i;
}
Console.WriteLine("The Factorial of " + num + " Is " + fact);

Algorithm to Calculate Factorial:

1. Initialize variables: num, fact = 1.
2. Print the message "Enter Integer" to prompt the user.
3. Read an integer from the user and store it in the variable num.
4. Start a for loop with the loop variable i initialized to 1, and the loop condition i <= num.
a. Inside the loop, multiply the current value of fact by i and update the value of fact.
5. After the loop, print "The Factorial of {num} Is {fact}" to display the calculated factorial.
6. End.

Note: Factorial of a non-negative integer n is the product of all positive integers less than or equal to n, denoted by n!.

Q8: Write a program to find the duplicate element in the Array.

            int[] arr = { 1, 2, 3, 4, 5, 6, 6, 3, };

for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] == arr[j])
{
Console.WriteLine(arr[i]);
}
}
}

Q9: Write a program to find the duplicate elements in String.

           string str = "aaetioommrl";

char[] ch = str.ToCharArray();
for (int w = 0; w < ch.Length; w++)
{
for (int y = w + 1; y < ch.Length; y++)
{
if (ch[w] == ch[y])
{
Console.WriteLine(ch[w]);
}
}
}

Q10: Write a program to find the value max, min, sum, Sort, Reverse or average in an Array.

           int[] nums = new int[5] { 10, 15, 16, 8, 6 };

Array.Sort(nums);
Console.WriteLine(nums.Reverse());
Console.WriteLine(nums.Max());
Console.WriteLine(nums.Min());
Console.WriteLine(nums.Sum());
Console.WriteLine(nums.Average());

Q11: Write a program to find the second largest number in an Array.

           int max1, max2;

int[] arr = new int[] {12,15,88,96,45,22};
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]+ "");
}
max1 = max2 = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i]>max1)
{
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max2)
{
max2= arr[i];
}
}
Console.WriteLine("The largest Number in Array is :" +max1);
Console.WriteLine("The Secod largest Number in Array is :" +max2);



Algorithm FindTwoLargestNumbers(arr):
1. max1 = max2 = arr[0]
2. for i = 0 to length - 1 do
a. Print arr[i]
3. end for
4. for i = 0 to length - 1 do
a. if arr[i] > max1 then
i. max2 = max1
ii. max1 = arr[i]
b. else if arr[i] > max2 then
i. max2 = arr[i]
5. end for
6. Print "The largest Number in Array is : " + max1
7. Print "The Second largest Number in Array is : " + max2

Q12: Write a program to find Palindram Number.

            int num, temp, rem, rev=0;

Console.WriteLine("Enter Integer");
num= Convert.ToInt32(Console.ReadLine());
temp = num;
while (num > 0)
{
rem = num % 10;
rev = rev*10 + rem;
num = num / 10;

}
if (rev == temp)
{
Console.WriteLine("Number are Palindram");
}
else
{
Console.WriteLine("Nuber Are not Palindrom");
}

Q13: Write a program to find Prime Number.

            Console.WriteLine("Enter Integer");
int num = Convert.ToInt32(Console.ReadLine());
bool isPrime = true;
for (int i = 2; i < num/2; i++)
{
if (num % i == 0)
{
isPrime = false; break;
}
}
if (isPrime)
{
Console.WriteLine("The Integer is Prime Number");
}
else
{
Console.WriteLine("The Integer is not a Prime Number");

}


Algorithm to Check if an Integer is Prime:

1. Print the message "Enter Integer" to prompt the user.
2. Read an integer from the user and store it in the variable num.
3. Initialize a boolean variable isPrime to true (assuming the number is prime until proven otherwise).
4. Start a for loop with the loop variable i initialized to 2, and the loop condition i < num / 2.
a. Inside the loop, check if num is divisible evenly by i (num % i == 0).
i. If true, set isPrime to false and break out of the loop.
5. After the loop, check the value of isPrime.
a. If isPrime is true, print "The Integer is Prime Number."
b. If isPrime is false, print "The Integer is not a Prime Number."
6. End.

Note: A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The algorithm iterates from 2 to num / 2 to check for divisors. If any divisor is found, the number is not prime.

Q15: How to remove duplicate characters from a string?

// Remove All duplicate Element in array
string str ="aadsssffdukijhhh";
string result = string.Empty;

for (int i = 0; i < str.Length; i++)
{
if (!result.Contains(str[i]))
{
result += str[i];
}
}
Console.WriteLine(result);

Q16: Write a program to Remove Duplicate string Elements.

//Remove duplicate element in string
string myStr = "aaetioommrl";
var unique = new HashSet<char>(myStr);
foreach (var item in unique)
Console.WriteLine(item);

Q17: Write a program to Reverse the String Element.

           string str, revers = "";
Console.WriteLine("Enter String");
str = Console.ReadLine();
str = str.ToLower();
for (int i = str.Length-1; i>=0; i--)
{
revers = revers + str[i];
}
Console.WriteLine("The String is {0}:",revers);



Algorithm ReverseString():
1. Print "Enter String"
2. Read input into the string variable `str`
3. Convert `str` to lowercase
4. Initialize an empty string variable `revers`
5. for i = length - 1 to 0 do
a. Append str[i] to `revers`
6. end for
7. Print "The Reversed String is: " + `revers`

Q18: Write a program to swap two Number using without a third variable.

            int a = 5, b = 10;
Console.WriteLine("Before swap a= " + a + " b= " + b);
a = a * b; //a=50 (5*10)
b = a / b; //b=5 (50/10)
a = a / b; //a=10 (50/5)
Console.Write("After swap a= " + a + " b= " + b);

Q18: Write a program to Counting Vowels In an Array.

//Count Vowel in array
int vowel = 0;
int cons = 0;
string[] arr = new string[] { "'a','A','e','E','i','I','o','O','g','m'" };
foreach (var s in arr) // s is a string
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
vowel++;
}
else
cons++;
}
}
Console.Write("\nVowels in the string: {0}\n", vowel);

Q19: Write a program to Counting Vowels in string.

//Count vowel in string
string myStr = "Avengers";
int len, vowel_count=0, cons_count=0;

// find length
len = myStr.Length;
for (int i = 0; i < len; i++)
{
if (myStr[i] == 'a' || myStr[i] == 'e' || myStr[i] == 'i' || myStr[i] == 'o' || myStr[i] == 'u' || myStr[i] == 'A' || myStr[i] == 'E' || myStr[i] == 'I' || myStr[i] == 'O' || myStr[i] == 'U')
{
vowel_count++;
}
else
{
cons_count++;
}
}
Console.Write("\nVowels in the string: {0}\n", vowel_count);

Q20: Write a program to find missing numbers in an Array.

     int[] arr = { 1, 2, 4, 5,6 };           
int sum = (arr.Length+1)*(arr.Length+2)/2;
for(int i=0; i<arr.Length; i++)
sum -=arr[i];
Console.Write("Missing number is "+sum);

Q21: Write a program if the number is divisible by 3 then print “techno” if the number is divisible by 5 then print “Soft” or if the number is divisible by 3 and 5 then print “Solution”?

            int input;

if(input % 3 == 0)
{
Console.Write("Tecno");
}
if(input % 5 == 0)
{
Console.Write("Soft");
}
if (input % 5 == 0 && input % 3 ==0)
{
Console.WriteLine("Solution");
}

Q22: Write a program to print 100 to 1 numbers without using a loop.

        static void Print(int first , int last)
{
if(first >= last)
{
Console.WriteLine(first);
first--;
Print(first,last);
}
}

//Call Method in Main Class

Print(100, 1);

Q23: Write C# code for replace the value at index 3 with the sum of left and right element the array.

int[] array = { 1, 7, 2, 6, 4, 5 }; // Sample array

// Calculate the sum of elements at indices 0, 1, and 5
int leftSum = 0;
for (int i = 1; i <array.Length-3 ; i++)
{
leftSum += array[i];
}

int rightSum = 0;
for (int i = array.Length - 1; i >= array.Length - 2; i--)
{
rightSum += array[i];
}

// Replace the value at index 3 with the sum of leftSum and rightSum
array[3] = leftSum + rightSum;


// Print the updated array
Console.WriteLine("Updated Array:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}

Q24: Write a code for find Pivot Index in Array.

using System;
using System.Linq;

class GFG {
static int equilibrium(int[] arr)
{
var sumLeft = 0;
var sumRight = 0;

for(int i = 1; i<arr.Length; i++){
sumLeft += arr[i-1];
sumRight -= arr[i];
if(sumRight==sumLeft) return i;
}
return -1;
}

// Driver code
public static void Main()
{
int[] arr = {4,3,2,7,8,2,3,1};

Console.Write(equilibrium(arr));
}
}

Q25: Write a code for the occurrence of a number in an array using C# and for loop.

static void Main()
{
int[] array = { 1, 0, 4, 5, 0, 5, 0, 8 };

Console.WriteLine("Original Array:");
var dist = new Dictionary<int,int>();
foreach(var value in arr)
{
if(dist.contaiKey(value))
dist[value]++;
else
dist[value]=1;
}
foreach(var pair in dist)
{
console.WriteLine("{0}={1} time(s)",pair.key,pair.value);
}

}




Algorithm CountOccurrences(array):
1. dist = Initialize empty Dictionary<int, int>
2. foreach value in array do
a. if dist containsKey(value) then
i. dist[value]++
b. else
i. dist[value] = 1
3. end foreach
4. foreach pair in dist do
a. Print "{pair.Key}={pair.Value} time(s)"
5. end foreach

Q26: Find the product of the frequency of all the characters in the string Which us s =aaabc.

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
string s = "aaabc";
int N = 6;
int product = CalculateFrequencyProduct(s, N);
Console.WriteLine("Product of character frequencies: " + product);
}

static int CalculateFrequencyProduct(string s, int N)
{
Dictionary<char, int> frequencies = new Dictionary<char, int>();

// Count frequencies of characters
foreach (char c in s)
{
if (frequencies.ContainsKey(c))
{
frequencies[c]++;
}
else
{
frequencies[c] = 1;
}
}

// Calculate product of frequencies raised to power of N
int product = 1;
foreach (var kvp in frequencies)
{
product *= (int)Math.Pow(kvp.Value, N);
}

return product;
}
}

--

--