First time user?    |

#### Select chapter

functions (3.papers)
loops (1.papers)
arrays (2.papers)
structures (3.papers)
unions (1.papers)
pointers (7.papers)
null pointers (1.papers)
strings (2.papers)
misc c concepts (1.papers)
Data structures (5.papers)
trees (1.papers)
basics (5.papers)
Hide

# c interview questions and answers

1
`In bubble sort how many number of times swapping is done in the best case?`
1. `0`
2. `1`
3. `n`
4. `n-1(n/2)`
Share
2
```What is below function fun doing?

#include  <stdio.h>
void fun(int item, int n);

int arr[10] = {0,1,2,3,7,12,23,24,90};

int main()
{
fun(17, 9);

for(int j=0; j<10; j++){
printf("%d ", arr[j]);
}
return 0;
}

void fun(int item, int n){
int i;
for(i=n-1; i>=0 && item<arr[i]; i--){
arr[i+1] = arr[i];
}
arr[i+1] = item;
}```
1. `it is finding if the given number is available in the array or not. if it is available, then it is adding 1 to that number and replacing the same.`
2. `it is trying to insert element 9, at the location 17 in the array.`
3. `it is trying to insert item 17, in this given sorted array of current size 9. but actual size of this array is 10. since array is in sorted order, it inserts it before 23.`
4. `it is trying to insert item 17, in this given sorted array of current size 10. since array is in sorted order, it inserts it before after 12, after traversing through 12.`
Share
Share
4
`What is the worst case time complexity of insertion sort? (i.e number of comparisons?)`
1. `(n-1)n/2`
2. `n`
3. `2(n^2)`
4. `2(n log n)`
Share
5
```what is below function fun() is doing?

#include  <stdio.h>

int arr[10] = {0,1,2,3,7,12,17,23,24,90};

int main(void)
{
int i = fun(7,10);
return 0;
}

int fun(int item, int n){
int l=0, r=n-1;
while(l<=r){
int m = (l+r)/2;
if(item == arr[m]){
return m;
}
if(item > arr[m]){
l = m+1;
}else{
r = m-1;
}
}
return -1;
}```
1. `it is trying to insert a new element 7 in the given array of size 10 in the appropriate location so that resultant array will be still in sorted order.`
2. `it is trying to find given element 7 if it is available in the given array of 10 elements using linear search algorithm.`
3. `it is trying to find given element 7 if it is available in the given array of 10 elements using binary search algorithm.`
4. `it is trying to insert a new element 7 in the given array of size 10 in the last location.`
Share
Share
7
`What is the worst case time complexity of a linear search algorithm?`
1. `O(n)`
2. `O(n log n)`
3. `O(log n)`
4. `O(n^2)`
Share
8
`What is the worst case time complexity of a linear search algorithm, if array is already sorted?`
1. `O(n log n)`
2. `O(log n)`
3. `O(n)`
4. `O(n log n^2)`
Share
Share
Share
Share
12
`Which of the below sorting algorithms follow divide and conquer mechanism`
1. `bubble sort`
2. `insertion sort`
3. `merge sort`
4. `selection sort`
Share
13
`I wanted store all the words of an English dictionary along with their meanings. Then what is the suitable data structures I have to use for it, to store efficiently so that I can insert and find a word meaning easily.`
1. `Use array to store all the meanings of all the words, with the help of hash code for each word [or] key.`
2. `create a separate linked list for each word, where each linked list will have all the synonyms of a given word. and maintain all the words in an array.`
3. `Use hash table (table of buckets) technique, where each bucket will contain linked list of entries for words with key,value pairs. each word will have a pointer to particular bucket based on its hash code.`
4. `User binary tree to insert all the words, and in the leaf nodes maintain a pointer to a linked list which will contain synonyms for that word.`
Share
14
`Which of the below sorting algorithms follow divide and conquer mechanism`
1. `Linear search`
2. `Binary search`
3. `both option 1 and 2`
4. `none`
Share
15
`Which of the below algorithms it is not possible to follow divide and conquer mechanism`
1. `finding power of a number`
2. `Finding fibonacci series`
3. `quick sort`
4. `none`
Share
16
`What is the worst case time complexity of a binary search algorithm?`
1. `O(n)`
2. `O(log n)`
3. `O(n log n)`
4. `O(1)`
Share
Share
18
```What is the problem with below sorting algorithm? Can you optimize this?

#include  <stdio.h>

int arr[10] = {12,2,3,0,1,17,23,90,24,7};

int main(void)
{
sort(10);
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
return 0;
}

void sort(int size){
for(int i=size; i>1; i--){
int k = max(i);
int temp = arr[k];
arr[k] = arr[i-1];
arr[i-1] = temp;

}
}

int max(int size){
int pos = 0;
for(int i=1; i<size; i++)
{
if(arr[pos] < arr[i])
pos = i;
}
return pos;
}```
1. `There is no problem with this sorting algorithm. if you want optimization then go for merge sort.`
2. `the problem with this sorting algorithm, in the worst case if the array is already in sorted order, then unnecessarily we are looping through the array for (n-1)n/2 times. To optimize this, we can put a check in max() function, if all elements are in sorted order, then maintaining one boolean flag so that outer for loop will keep checking that flag to stop iterating.`
3. `the problem with this sorting algorithm, in the worst case if the array is already in sorted order, then unnecessarily we are looping through the array for (n-1)n/2 times. To optimize this, we better use merge sort algorithm it takes only O(n log n) iterations to sort the array elements.`
4. `the problem with this sorting algorithm, in the worst case if the array is already in sorted order, then unnecessarily we are looping through the array for (n-1)n/2 times. To optimize this, we can put a check in sort() function, if all elements are in sorted order, then maintaining one boolean flag so that outer for loop will keep checking that flag to stop iterating.`
Share
Share
20
`What is the best case time complexity of above mentioned question improvised selection sort algorithm?`
1. `n-1 comparisons + 3(n-1) swaps`
2. `n-1(n/2) comparisons + 3(n-1) swaps`
3. `log n comparisons + 3(n-1) swaps`
4. `n log n comparisons + 3(n-1) swaps`