Miscellaneous On Data Structures - Study Mode

[#1021] Which of the following code will give an error?
Correct Answer

(A) #include <stdlib.h> int main() { printf("%d ", srand()) return 0 }

[#1022] What does the following recursive code do? void my_recursive_function(int n)
{
if(n == 0)
return

my_recursive_function(n-1)

printf("%d ",n)

}
int main()
{
my_recursive_function(10)

return 0

}
Correct Answer

(C) Prints the numbers from 1 to 10

[#1023] Which of the following should be the base case for the recursive solution of a set partition problem?
Correct Answer

(B) If(sum%2!=0) return false if(sum==0) return true if (n ==0 && sum!= 0) return false

[#1024] What is the time complexity of the following code used to find the length of a linked list? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val

struct Node *next

}*head

int recursive_get_len(struct Node *current_node)
{
if(current_node == 0)
return 0

return 1 + recursive_get_len(current_node->next)

}
int main()
{
int arr[10] = {-1,2,3,-3,4,5,0}, n = 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->val = arr[i]

newNode->next = 0

temp->next = newNode

temp = temp->next

}
int len = recursive_get_len(head->next)

printf("%d",len)

return 0

}
Correct Answer

(B) O(n)

[#1025] What will be the time complexity of 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 log n)