Miscellaneous On Data Structures - Study Mode
[#1036] What is the output of the following code? int fibo(int n)
{
if(n == 1)
return 0
else if(n == 2)
return 1
return fibo(n - 1) + fibo(n - 2)
}
int main()
{
int n = -1
int ans = fibo(n)
printf("%d",ans)
return 0
}
Correct Answer
(D) Runtime error
[#1037] What is the auxiliary space complexity of the given code? #include <bits/stdc++.h>
using namespace std
void convert(int a[], int n)
{
vector <pair<int, int> > vec
for (int i = 0
i < n
i++)
vec.push_back(make_pair(a[i], i))
sort(vec.begin(), vec.end())
for (int i=0
i<n
i++)
a[vec[i].second] = i
}
void printArr(int a[], int n)
{
for (int i=0
i<n
i++)
cout << a[i] << " "
}
int main()
{
int arr[] = {10,8,2,5,7}
int n = sizeof(arr)/sizeof(arr[0])
convert(arr , n)
printArr(arr, n)
return 0
}
Correct Answer
(B) O(n)
[#1038] What is the time complexity of the following iterative implementation used to find the length of a linked list? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val
struct Node *next
}*head
int get_len()
{
struct Node *temp = head->next
int len = 0
while(temp != 0)
{
len++
temp = temp->next
}
return len
}
int main()
{
int arr[10] = {1,2,3,4,5}, n = 5, 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->val = arr[i]
newNode->next = 0
temp->next = newNode
temp = temp->next
}
int len = get_len()
printf("%d",len)
return 0
}
Correct Answer
(B) O(n)
[#1039] Consider the following recursive implementation of linear search on a linked list: struct Node
{
int val
struct Node* next
}*head
int linear_search(struct Node *temp,int value)
{
if(temp == 0)
return 0
if(temp->val == value)
return 1
return _________
} Which of the following lines should be inserted to complete the above code?
Correct Answer
(D) linear_search(temp->next, value)
[#1040] Consider the following recursive implementation to find the largest element in an array. int max_of_two(int a, int b)
{
if(a > b)
return a
return b
}
int recursive_max_element(int *arr, int len, int idx)
{
if(idx == len - 1)
return arr[idx]
return _______
} Which of the following lines should be inserted to complete the above code?
Correct Answer
(C) max_of_two(arr[idx], recursive_max_element(arr, len, idx + 1))