This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Largest and Smallest Number in a Linked List using Recursion”.
1. Which of the following methods can be used to find the largest and smallest number in a linked list?
a) Recursion
b) Iteration
c) Both Recursion and iteration
d) Impossible to find the largest and smallest numbers
View Answer
Explanation: Both recursion and iteration can be used to find the largest and smallest number in a linked list.
2. Consider the following code snippet to find the largest element in a linked list:
struct Node{ int val; struct Node *next; }*head; int get_max() { struct Node* temp = head->next; int max_num = temp->val; while(______) { if(temp->val > max_num) max_num = temp->val; temp = temp->next; } return max_num; }
Which of the following lines should be inserted to complete the above code?
a) temp->next != 0
b) temp != 0
c) head->next != 0
d) head != 0
View Answer
Explanation: The line “temp != 0” should be inserted to complete the above code.
3. Consider the following code snippet to find the smallest element in a linked list:
struct Node { int val; struct Node* next; }*head; int get_min() { struct Node* temp = head->next; int min_num = temp->val; while(temp != 0) { if(_________) min_num = temp->val; temp = temp->next; } return min_num; }
Which of the following lines should be inserted to complete the above code?
a) temp > min_num
b) val > min_min
c) temp->val < min_num
d) temp->val > min_num
View Answer
Explanation: The line “temp->val = min_num” should be inserted to complete the above code.
4. What is the output of the following code:
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int get_max() { struct Node* temp = head->next; int max_num = temp->val; while(temp != 0) { if(temp->val > max_num) max_num = temp->val; temp = head->next; } return max_num; } int main() { int n = 9, arr[9] ={5,1,3,4,5,2,3,3,1},i; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max_num = get_max(); printf("%d %d",max_num); return 0; }
a) 5
b) 1
c) runtime error
d) garbage value
View Answer
Explanation: The variable temp will always point to the first element in the linked list due to the line “temp = head->next” in the while loop. So, it will be an infinite while loop and the program will produce a runtime error.
5. What is the output of the following code?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int get_max() { struct Node* temp = head->next; int max_num = temp->val; while(temp != 0) { if(temp->val > max_num) max_num = temp->val; temp = temp->next; } return max_num; } int get_min() { struct Node* temp = head->next; int min_num = temp->val; while(temp != 0) { if(temp->val < min_num) min_num = temp->val; temp = temp->next; } return min_num; } int main() { int i, n = 9, arr[9] ={8,3,3,4,5,2,5,6,7}; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max_num = get_max(); int min_num = get_min(); printf("%d %d",max_num,min_num); return 0; }
a) 2 2
b) 8 8
c) 2 8
d) 8 2
View Answer
Explanation: The program prints the largest and smallest elements in the linked list, which are 8 and 2 respectively.
6. What is the time complexity of the following iterative code used to find the smallest and largest element in a linked list?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int get_max() { struct Node* temp = head->next; int max_num = temp->val; while(temp != 0) { if(temp->val > max_num) max_num = temp->val; temp = temp->next; } return max_num; } int get_min() { struct Node* temp = head->next; int min_num = temp->val; while(temp != 0) { if(temp->val < min_num) min_num = temp->val; temp = temp->next; } return min_num; } int main() { int i, n = 9, arr[9] ={8,3,3,4,5,2,5,6,7}; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max_num = get_max(); int min_num = get_min(); printf("%d %d",max_num,min_num); return 0; }
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Explanation: The time complexity of the above iterative code used to find the largest and smallest element in a linked list is O(n).
7. Consider the following recursive implementation to find the largest element in a linked list:
struct Node { int val; struct Node* next; }*head; int max_of_two(int a, int b) { if(a > b) return a; return b; } int recursive_get_max(struct Node* temp) { if(temp->next == 0) return temp->val; return max_of_two(______, _______); }
Which of the following arguments should be passed to the function max_of two() to complete the above code?
a) temp->val,recursive_get_max(temp->next)
b) temp, temp->next
c) temp->val, temp->next->val
d) temp->next->val, temp
View Answer
Explanation: The arguments {temp->val,recursive_get_max(temp->next)} should be passed to the function max_of_two() to complete the above code.
8. What is the output of the following code?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int max_of_two(int a, int b) { if(a > b) return a; return b; } int recursive_get_max(struct Node* temp) { if(temp->next == 0) return temp->val; return max_of_two(temp->val,recursive_get_max(temp->next)); } int min_of_two(int a, int b) { if(a < b) return a; return b; } int recursive_get_min(struct Node* temp) { if(temp->next == 0) return temp->val; return min_of_two(temp->val,recursive_get_min(temp->next)); } int main() { int n = 9, arr[9] ={1,3,2,4,5,0,5,6,7},i; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max_num = recursive_get_max(head->next); int min_num = recursive_get_min(head->next); printf("%d %d",max_num,min_num); return 0; }
a) 7 1
b) 0 7
c) 7 0
d) 1 1
View Answer
Explanation: The program prints the largest and the smallest elements in the linked list, which are 7 and 0 respectively.
9. What is the time complexity of the recursive implementation used to find the largest and smallest element in a linked list?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int max_of_two(int a, int b) { if(a > b) return a; return b; } int recursive_get_max(struct Node* temp) { if(temp->next == 0) return temp->val; return max_of_two(temp->val,recursive_get_max(temp->next)); } int min_of_two(int a, int b) { if(a < b) return a; return b; } int recursive_get_min(struct Node* temp) { if(temp->next == 0) return temp->val; return min_of_two(temp->val,recursive_get_min(temp->next)); } int main() { int n = 9, arr[9] ={1,3,2,4,5,0,5,6,7},i; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max_num = recursive_get_max(head->next); int min_num = recursive_get_min(head->next); printf("%d %d",max_num,min_num); return 0; }
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Explanation: The time complexity of the above recursive implementation used to find the largest and smallest element in linked list is O(n).
10. What is the output of the following code?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int min_of_two(int a, int b) { if(a < b) return a; return b; } int recursive_get_min(struct Node* temp) { if(temp->next == 0) return temp->val; return min_of_two(temp->val,recursive_get_min(temp->next)); } int main() { int n = 5, arr[5] ={1,1,1,1,1},i; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int min_num = recursive_get_min(head->next); printf("%d",min_num); return 0; }
a) 1
b) 0
c) compile time error
d) runtime error
View Answer
Explanation: The program prints the smallest element in the linked list, which is 1.
11. How many times will the function recursive_get_min() be called when the following code is executed?
#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int min_of_two(int a, int b) { if(a < b) return a; return b; } int recursive_get_min(struct Node* temp) { if(temp->next == 0) return temp->val; return min_of_two(temp->val,recursive_get_min(temp->next)); } int main() { int n = 5, arr[5] ={1,1,1,1,1},i; struct Node *temp, *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int min_num = recursive_get_min(head->next); printf("%d",min_num); return 0; }
a) 4
b) 5
c) 6
d) 7
View Answer
Explanation: The function recursive_get_min() will be called 5 times when the above code is executed.
Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.
To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Programming Books
- Apply for Computer Science Internship
- Practice Data Structure MCQ
- Practice Programming MCQs
- Check Computer Science Books