Interview Programming Questions and Answers in C — Part 3

Interview Programming Questions and Answers in C

Q. W.A.P to find the maximum/largest element without using an array concept.
A.

#include <stdio.h>int main()
{
int size, element, largest = 0;

printf("Enter The Number of Elements :: ");
scanf("%d", &size);

printf("Enter %d Integers :: \n", size);

for(int i = 0; i < size; i++)
{
scanf("%d", &element);

if(element > largest)
{
largest = element;
}
}

printf("Largest number is %d \n", largest);

return 0;
}

Q. W.A.P to find the maximum/largest element in an array.
A.

#include <stdio.h>int main()
{
int array[100], size, i, maximum, location = 1;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Integers :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

maximum = array[0];

for(i = 1; i < size; i++)
{
if(array[i] > maximum)
{
maximum = array[i];
location = i+1;
}
}

printf("Maximum Element is Present At Location %d and It's Value is %d \n", location, maximum);
return 0;
}

Q. W.A.P to find the minimum/smallest element in an array.
A.

#include <stdio.h>int main()
{
int array[100], size, i, minimum, location = 1;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Integers :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

minimum = array[0];

for(i = 1; i < size; i++)
{
if(array[i] < minimum)
{
minimum = array[i];
location = i+1;
}
}

printf("Minimum Element is Present At Location %d and It's Value is %d \n", location, minimum);
return 0;
}

Q. W.A.P to insert an element in an array.
A.

#include <stdio.h>int main()
{
int array[100], size, position, value, i;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Elements :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the Location to Insert :: ");
scanf("%d", &position);

printf("Enter the Value to Insert :: ");
scanf("%d", &value);
for(i = size-1; i >= position-1; i--)
{
array[i+1] = array[i];
}

array[position-1] = value;
printf("Resultant Array is :: \n");

for(i = 0; i <= size; i++)
{
printf("%d\n", array[i]);
}

return 0;
}

Q. W.A.P to delete an element from an array.
A.

#include <stdio.h>int main()
{
int array[100], size, position, i;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Elements :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

printf("Enter the Location to Delete :: ");
scanf("%d", &position);

if(position > size)
{
printf("Deletion Not Possible. \n");
}
else
{
for(i = position - 1; i < size - 1; i++)
{
array[i] = array[i+1];
}


printf("Resultant Array is :: \n");

for(i = 0 ; i < size - 1 ; i++)
{
printf("%d \n", array[i]);
}
}

return 0;
}

Q. W.A.P to remove duplicate elements from an array.
A.

#include <stdio.h>void main()
{
int a[20], i, j, k, n;
printf("Enter array size :: ");
scanf("%d", &n);
printf("Enter %d array element :: \n", n);

for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Original array is :: \n");
for(i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
for(i = 0; i < n; i++)
{
for(j = i+1; j < n;)
{
if(a[j] == a[i])
{
for(k = j; k < n; k++)
{
a[k] = a[k+1];
}
n--;
}
else
{
j++;
}
}
}
printf("New array is :: \n");
for(i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}

}

Q. W.A.P to merge two sorted arrays.
A. It is assumed that the user will enter arrays in ascending order.

#include <stdio.h>

int main()
{
int a[8] = {10,15,18,20,25,30,35,40};
int b[9] = {1,6,19,22,28,45,50,51,53};
int c[20];
int i, j, k, n1, n2;
i = j = k = 0;
n1 = 8;
n2 = 9;

while(i < n1 && j < n2)
{
if(a[i] < b[j])
{
c[k++] = a[i++];
}
else
{
c[k++] = b[j++];
}
}
if(i < n1)
{
while(i < n1)
{
c[k++] = a[i++];
}
}
if(j < n2)
{
while(j < n2)
{
c[k++] = b[j++];
}
}
printf("Sorted Array is :: \n"); for(i = 0; i < k; i++)
{
printf("%d\n", c[i]);
}
}

Q. W.A.P to merge two unsorted arrays.
A.

#include <stdio.h>int main()
{
int m, n, i = 0, j = 0, temp;

printf("Enter The Number of Elements in First Array :: ");
scanf("%d", &m);

printf("Enter The Number of Elements in Second Array :: ");
scanf("%d", &n);

int c[m+n];

printf("Enter %d Integers For First Array \n", m);
for(i = 0; i < m; i++)
{
scanf("%d", &c[i]);
}

printf("Enter %d Integers For Second Array \n", n);
for(i = i; i < m+n; i++)
{
scanf("%d", &c[i]);
}

printf("Start Sorting \n");
for(i = 0; i < m+n; i++)
{
for(j = i+1; j < m+n; j++)
{
if(c[i] > c[j])
{
temp = c[i];
c[i] = c[j];
c[j] = temp;
}

}
}

printf("Elements of Array After Merging and Sorting :: \n");
for(i = 0; i < m+n; i++)
{
printf("%d\n", c[i]);
}

return 0;
}

Q. W.A.P to merge elements of two arrays alternatively without using the third array.
A.

#include <stdio.h>int main()
{
int a[10] = {10,20,30,40,50};
int b[5] = {15,25,35,45,55};
int i, j, k, n1, n2;
k = 1;
n1 = 5;
n2 = 5;

for(j = 0; j < n2; j++)
{
for(i = n1-1; i >= k; i--)
{
a[i+1] = a[i];
}

a[k] = b[j];

k = k + 2;
n1 = n1 + 1;
}
printf("Final Array is :: \n");
for(i = 0; i < n1; i++)
{
printf("%d\n", a[i]);
}
}

Q. W.A.P to print the largest and the second largest element of the array.
A.

#include <stdio.h>void main()
{
int arr[100], i, n, largest1, largest2;
printf("Enter the size of the array :: ");
scanf("%d", &n);
printf("Enter the elements of the array :: \n"); for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
largest1 = arr[0];
largest2 = arr[1];
for(i = 0; i < n; i++)
{
if(arr[i] > largest1)
{
largest2 = largest1;
largest1 = arr[i];
}
else if (arr[i] > largest2 && arr[i] != largest1)
{
largest2 = arr[i];
}
}
printf("largest = %d, second largest = %d", largest1, largest2);
}

Q. W.A.P to print the first largest, second largest, and third largest element of the array.
A.

#include <stdio.h>void main()
{
int arr[100], i, n, largest1, largest2, largest3;
printf("Enter the size of the array :: ");
scanf("%d", &n);
printf("Enter the elements of the array :: \n"); for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
largest1 = arr[0];
largest2 = arr[1];
largest3 = arr[2];
for(i = 0; i < n; i++)
{
if(arr[i] > largest1)
{
largest3 = largest2;
largest2 = largest1;
largest1 = arr[i];
}
else if (arr[i] > largest3 && arr[i] != largest1 && arr[i] != largest2)
{
largest3 = arr[i];
}
else if (arr[i] > largest2 && arr[i] != largest1 && arr[i] != largest3)
{
largest2 = arr[i];
}
}

printf("First Largest = %d, Second Largest = %d, Third Largest = %d", largest1, largest2, largest3);
}

Q. W.A.P to reverse an array without using the second array.
A.

#include <stdio.h>int main()
{
int array[100], n, i, temp, end;

printf("Enter the size of the array :: ");
scanf("%d", &n);
printf("Enter the elements of the array :: \n");

for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}

end = n - 1;

for (i = 0; i < n/2; i++)
{
temp = array[i];
array[i] = array[end];
array[end] = temp;
end--;
}

printf("Reversed array elements are :: \n");

for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}

return 0;
}

or

#include <stdio.h>int main()
{
int a[100], n, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &n);
printf("Enter the elements of the array :: \n");

for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}



i = 0;
j = n - 1;

while (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}

printf("Reversed array elements are :: \n");

for (i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}

return 0;
}

Q. W.A.P to arrange even and odd numbers in the random array.
A. Segregate even and odd numbers in the array such that all the even numbers should be present first, and then the odd numbers.

#include <stdio.h>int main()
{
int a[100], size, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &a[i]);
}


i = 0;
j = size - 1;

while (i < j)
{
while (a[i] % 2 == 0 && i < j)
{
i++;
}

while (a[j] % 2 != 0 && i < j)
{
j--;
}

if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}

printf("Final array elements are :: \n");

for (i = 0; i < size; i++)
{
printf("%d\n", a[i]);
}

return 0;
}

Q. W.A.P to sort elements of an array through Bubble Sort.
A.
Algorithm:
Step 1: Compare the first and the second element of the array and swap them if they are in the wrong order.
Step 2: Compare the second and the third element of the array and swap them if they are in the wrong order.
Step 3: Proceed until the last element of the array in a similar fashion.
Step 4: Repeat all of the above steps until the array is sorted.

#include <stdio.h>int main()
{
int arr[100], size, i, j, temp, flag;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nSorting array using bubble sort technique...\n");
for(i = 0; i < (size-1); i++)
{
flag = 0;

for(j = 0; j < (size-1-i); j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = 1;
}
}

if(flag == 0)
{
break;
}
}

printf("All Array elements sorted successfully!\n");
printf("Array elements in ascending order:\n\n");

for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

Q. W.A.P to sort elements of an array through Selection Sort.
A.
Algorithm:
Step 1: Set min to the first location.
Step 2: Search the minimum element in the array.
Step 3: Swap the first location with the minimum value in the array.
Step 4: Assign the second element as min.
Step 5: Repeat the process until we get a sorted array.

#include <stdio.h>int main()
{
int arr[100], size, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nSorting array using selection sort technique...\n");
for(i = 0; i < size-1; i++)
{
int min = i;

for(j = i+1; j < size; j++)
{
if(arr[j] < arr[min])
{
min = j;
}
}

if(min != i)
{
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}

printf("All Array elements sorted successfully!\n");
printf("Array elements in ascending order:\n\n");

for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

Q. W.A.P to sort elements of an array through Insertion Sort.
A.

#include <stdio.h>int main()
{
int arr[100], size, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nSorting array using insertion sort technique...\n");
for(i = 1; i < size; i++)
{
temp = arr[i];
j = i-1;

while(j >= 0 && arr[j] > temp)
{
arr[j+1] = arr[j];
j--;
}

arr[j+1] = temp;
}

printf("All Array elements sorted successfully!\n");
printf("Array elements in ascending order:\n\n");

for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

Q. W.A.P to sort elements of an array through Quick Sort.
A. Quicksort is based on the ‘Divide & Conquer’ algorithm. In this sorting technique, an element is picked as a pivot and the array is partitioned around the pivot element. The target of each partition is, to put all smaller elements before pivot & put all greater elements after the pivot.

Now, what is ‘Divide & Conquer’?
In the Divide & Conquer algorithm design paradigm, we divide the problems into sub-problems recursively then solve the sub-problems, & at last combine the solutions to find the final result.
One thing to keep in mind while dividing the problems into sub-problems is that the structure of sub-problems should not change as of the original problem.
Divide & Conquer algorithm has 3 steps:
1. Divide: Breaking the problem into subproblems
2. Conquer: Recursively solving the subproblems
3. Combine: Combining the solutions to get the final result

Now first, we look into the pseudocode for recursive QuickSort function :

/* low  --> Starting index,  high  --> Ending index */
QuickSort(arr[], low, high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is now
at right place */
int pi = partition(arr, low, high);

QuickSort(arr, low, pi - 1); // Before pi
QuickSort(arr, pi + 1, high); // After pi
}
}
--------------------------------------------------------------------/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
partition (arr[], low, high)
{
pivot = arr[low];

start = low;
end = high;

while(start < end)
{
while(arr[start] <= pivot)
{
start++;
}

while(arr[end] > pivot)
{
end--;
}

if(start < end)
{
swap(arr[start], arr[end])
}
}

swap(arr[low], arr[end])
return end;
}

A program for the above Pseudocode is given below.

#include <stdio.h>void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void swap(int *a, int *b);
int main()
{
int arr[100], size, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nSorting array using quick sort technique... \n");
quickSort(arr, 0, size-1);

printf("All Array elements sorted successfully! \n");
printf("Array elements in ascending order :: \n\n");

for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int start = low;
int end = high;

while(start < end)
{
while(arr[start] <= pivot)
{
start++;
}

while(arr[end] > pivot)
{
end--;
}

if(start < end)
{
swap(&arr[start], &arr[end]);
}
}

swap(&arr[low], &arr[end]);
return end;
}
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}

Q. W.A.P to sort elements of an array through Merge Sort.
A. Mergesort is also based on the ‘Divide & Conquer’ algorithm. In Merge sort, we divide the array recursively in two halves, until each sub-array contains a single element, and then we merge the sub-array in a way that it results in a sorted array.

Merge sort is one of the efficient & fastest sorting algorithms with the following time complexity:

Worst Case Time Complexity: O(n*log n)
Best Case Time Complexity: O(n*log n)
Average Time Complexity: O(n*log n)

#include <stdio.h>void mergeSort(int arr[], int low, int high);
void merge(int arr[], int low, int mid, int high);
int main()
{
int arr[100], size, i, j, temp;

printf("Enter the size of the array :: ");
scanf("%d", &size);

printf("Enter the elements of the array :: \n");

for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nSorting array using merge sort technique... \n");
mergeSort(arr, 0, size-1);

printf("All Array elements sorted successfully! \n");
printf("Array elements in ascending order :: \n\n");

for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
void mergeSort(int arr[], int low, int high)
{
if (low < high)
{
int mid = (low + high)/2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);

merge(arr, low, mid, high);
}
}
void merge(int arr[], int low, int mid, int high)
{
int i = low; // Initial index of first subarray
int j = mid + 1; // Initial index of second subarray
int k = low; // Initial index of merged subarray
int arrNew[100];

while(i <= mid && j <= high)
{
if(arr[i] <= arr[j])
{
arrNew[k] = arr[i];
i++;
}
else
{
arrNew[k] = arr[j];
j++;
}

k++;
}

if(i > mid)
{
while(j <= high)
{
arrNew[k] = arr[j];
j++;
k++;
}
}
else
{
while(i <= mid)
{
arrNew[k] = arr[i];
i++;
k++;
}
}

//Transfer elements from arrNew[] back to arr[]
for(i = 0; i <= high; i++)
{
arr[i] = arrNew[i];
}
}

Q. W.A.P to find an element from an array through Linear/Sequential Search.
A.

#include <stdio.h>int main()
{
int array[100], size, number, i;
int found = 0;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Integers :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

printf("Enter a number to search :: ");
scanf("%d", &number);

for(i = 0; i < size; i++)
{
if(array[i] == number)
{
printf("Number is Present At Location %d.", i+1);
found = 1;
break;
}
}

if(found == 0)
{
printf("Number not found.");
}

return 0;
}

Q. W.A.P to find an element from a sorted array through Binary Search.
A.

#include <stdio.h>int binarySearch(int arr[], int low, int high, int num);
int binarySearchRecursive(int arr[], int low, int high, int num)
int main()
{
int array[100], size, number, i;

printf("Enter The Number of Elements in Array :: ");
scanf("%d", &size);

printf("Enter %d Integers :: \n", size);

for(i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

printf("Enter a number to search :: ");
scanf("%d", &number);

int result = binarySearch(array, 0, size - 1, number);

if(result == -1)
{
printf("Number not found.");
}
else
{
printf("Number is Present At Location %d.", result+1);
}

return 0;
}
int binarySearch(int arr[], int low, int high, int num)
{
while(low < high)
{
int mid = (low + high)/2;

if(num == arr[mid])
{
return mid;
}
else if(num < arr[mid])
{
high = mid - 1;
}
else if(num > arr[mid])
{
low = mid + 1;
}
}

// We reach here when element is not
// present in array
return -1;
}
int binarySearchRecursive(int arr[], int low, int high, int num)
{
if (high >= low)
{
int mid = (low + high) / 2;

if (arr[mid] == num)
{
return mid;
}

// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > num)
{
return binarySearch(arr, low, mid - 1, num);
}

// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, high, num);
}

// We reach here when element is not
// present in array
return -1;
}

.

“The best way to learn a new programming language is by writing programs in it.” — Dennis Ritchie

Thank you for reading! If you liked this article, please clap to get this article seen by more people.
Please follow me on Medium by clicking Follow.
I’m also active on LinkedIn, Twitter, and GitHub.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store