0% found this document useful (0 votes)
97 views

Dynamic Programming

The document provides information about an app that provides study materials and placement preparation resources. It includes a link to download the app from the Google Play store and describes some of the key features like getting latest exam updates, free study materials, and tips. There is also a short multiple choice question quiz on analysis of algorithms.

Uploaded by

Bhavin Vaghela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Dynamic Programming

The document provides information about an app that provides study materials and placement preparation resources. It includes a link to download the app from the Google Play store and describes some of the key features like getting latest exam updates, free study materials, and tips. There is also a short multiple choice question quiz on analysis of algorithms.

Uploaded by

Bhavin Vaghela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Download our App for Study Materials and Placement Preparation 📝✅ | Click Here 

(https://play.google.com/store/apps/details?id=co.jones.cjzgt)

Get Latest Exam Updates, Free Study materials and Tips


Your Name
(https://lastmomenttu
Your Branch
itions.com/)
Year Of Engineering

[MCQ] Analysis Of Algorithms

Introduction (#1617622154105-59ec4c41-97a6)

Divide and Conquer Approach (#1617622154112-c1392ccd-f510)

Greedy Method Approach (#1617629753056-e13c69b7-cef6)

Dynamic Programming Approach (#1617632450818-4e763a5a-bfbe)

 Module 4

1. The Bellmann Ford algorithm returns _______ value.


a) Boolean
b) Integer
c) String
d) Double
Answer: a
Explanation: The Bellmann Ford algorithm returns Boolean value whether there is a negative weight cycle that is reachable from the source.

2. Bellmann ford algorithm provides solution for ____________ problems.


a) All pair shortest path
b) Sorting
c) Network flow
d) Single source shortest path
Answer: d
Explanation: Bellmann ford algorithm is used for finding solutions for single source shortest path problems. If the graph has no negative cycles that
are reachable from the source then the algorithm produces the shortest paths and their weights.

3. Bellmann Ford algorithm is used to indicate whether the graph has negative weight cycles or not.
a) True
b) False
Answer: a
Explanation: Bellmann Ford algorithm returns true if the graph does not have any negative weight cycles and returns false when the graph has
negative weight cycles.

4. How many solution/solutions are available for a graph having negative weight cycle?
a) One solution
b) Two solutions
c) No solution
d) Infinite solutions
Answer: c
Explanation: If the graph has any negative weight cycle then the algorithm indicates that no solution exists for that graph.
5. What is the running time of Bellmann Ford Algorithm?
a) O(V)
b) O(V2)
c) O(ElogV)
d) O(VE)
Answer: d
Explanation: Bellmann Ford algorithm runs in time O(VE), since the initialization takes O(V) for each of V-1 passes and the for loop in the
algorithm takes O(E) time. Hence the total time taken by the algorithm is O(VE).

6. How many times the for loop in the Bellmann Ford Algorithm gets executed?
a) V times
b) V-1
c) E
d) E-1
Answer: b
Explanation: The for loop in the Bellmann Ford Algorithm gets executed for V-1 times. After making V-1 passes, the algorithm checks for a
negative weight cycle and returns appropriate boolean value.

7. Dijikstra’s Algorithm is more efficient than Bellmann Ford Algorithm.


a) True
b) False
Answer: a
Explanation: The running time of Bellmann Ford Algorithm is O(VE) whereas Dijikstra’s Algorithm has running time of only O(V2).

8. Identify the correct Bellmann Ford Algorithm.


a)

for i=1 to V[g]-1


do for each edge (u,v) in E[g]
do Relax(u,v,w)
for each edge (u,v) in E[g]
do if d[v]>d[u]+w(u,v)
then return False
return True
b)

for i=1 to V[g]-1


for each edge (u,v) in E[g]
do if d[v]>d[u]+w(u,v)
then return False
return True
c)

for i=1 to V[g]-1


do for each edge (u,v) in E[g]
do Relax(u,v,w)
for each edge (u,v) in E[g]
do if d[v]<d[u]+w(u,v)
then return true
return True
d)

for i=1 to V[g]-1


do for each edge (u,v) in E[g]
do Relax(u,v,w)
return True
Answer: a
Explanation: After initialization, the algorithm makes v-1 passes over the edges of the graph. Each pass is one iteration of the for loop and consists
of relaxing each edge of the graph once. Then it checks for the negative weight cycle and returns an appropriate Boolean value.

9. What is the basic principle behind Bellmann Ford Algorithm?


a) Interpolation
b) Extrapolation
c) Regression
d) Relaxation
Answer: d
Explanation: Relaxation methods which are also called as iterative methods in which an approximation to the correct distance is replaced
progressively by more accurate values till an optimum solution is found.

10. Bellmann Ford Algorithm can be applied for _____________


a) Undirected and weighted graphs
b) Undirected and unweighted graphs
c) Directed and weighted graphs
d) All directed graphs
Answer: c
Explanation: Bellmann Ford Algorithm can be applied for all directed and weighted graphs. The weight function in the graph may either be positive
or negative.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

11. Bellmann Ford algorithm was first proposed by ________


a) Richard Bellmann
b) Alfonso Shimbe
c) Lester Ford Jr
d) Edward F. Moore
Answer: b
Explanation: Alfonso Shimbe proposed Bellmann Ford algorithm in the year 1955. Later it was published by Richard Bellmann in 1957 and Lester
Ford Jr in the year 1956. Hence it is called Bellmann Ford Algorithm.

12. Consider the following graph. What is the minimum cost to travel from node A to node C?

a) 5
b) 2
c) 1
d) 3
Answer: b
Explanation: The minimum cost to travel from node A to node C is 2.
A-D, cost=1
D-B, cost=-2
B-C, cost=3
Hence the total cost is 2.

13. In the given graph, identify the path that has minimum cost to travel from node a to node f.

a) a-b-c-f
b) a-d-e-f
c) a-d-b-c-f
d) a-d-b-c-e-f
Answer: d
Explanation: The minimum cost taken by the path a-d-b-c-e-f is 4.
a-d, cost=2
d-b, cost=-2
b-c, cost=1
c-e, cost= 2
e-f, cost=1
Hence the total cost is 4.

14. Bellmann Ford Algorithm is an example for ____________


a) Dynamic Programming
b) Greedy Algorithms
c) Linear Programming
d) Branch and Bound
Answer: a
Explanation: In Bellmann Ford Algorithm the shortest paths are calculated in bottom up manner which is similar to other dynamic programming
problems.

15. A graph is said to have a negative weight cycle when?


a) The graph has 1 negative weighted edge
b) The graph has a cycle
c) The total weight of the graph is negative
d) The graph has 1 or more negative weighted edges
Answer: c
Explanation: When the total weight of the graph sums up to a negative number then the graph is said to have a negative weight cycle. Bellmann
Ford Algorithm provides no solution for such graphs.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

16. Which of the following methods can be used to solve the assembly line scheduling problem?
a) Recursion
b) Brute force
c) Dynamic programming
d) All of the mentioned
Answer: d
Explanation: All of the above-mentioned methods can be used to solve the assembly line scheduling problem.

17. What is the time complexity of the brute force algorithm used to solve the assembly line scheduling problem?
a) O(1)
b) O(n)
c) O(n2)
d) O(2n)
Answer: d
Explanation: In the brute force algorithm, all the possible ways are calculated which are of the order of 2n.

18. In the dynamic programming implementation of the assembly line scheduling problem, how many lookup tables are required?
a) 0
b) 1
c) 2
d) 3
Answer: c
Explanation: In the dynamic programming implementation of the assembly line scheduling problem, 2 lookup tables are required one for storing
the minimum time and the other for storing the assembly line number.

19. Consider the following assembly line problem:

time_to_reach[2][3] = {{17, 2, 7}, {19, 4, 9}}


time_spent[2][4] = {{6, 5, 15, 7}, {5, 10, 11, 4}}
entry_time[2] = {8, 10}
exit_time[2] = {10, 7}
num_of_stations = 4
For the optimal solution which should be the starting assembly line?
a) Line 1
b) Line 2
c) All of the mentioned
d) None of the mentioned
Answer: b
Explanation: For the optimal solution, the starting assembly line is line 2.

20. Consider the following assembly line problem:


time_to_reach[2][3] = {{17, 2, 7}, {19, 4, 9}}
time_spent[2][4] = {{6, 5, 15, 7}, {5, 10, 11, 4}}
entry_time[2] = {8, 10}
exit_time[2] = {10, 7}
num_of_stations = 4
For the optimal solution, which should be the exit assembly line?
a) Line 1
b) Line 2
c) All of the mentioned
d) None of the mentioned
Answer: b
Explanation: For the optimal solution, the exit assembly line is line 2.

Learn Machine Learning with Python from Scratch

Start your Machine learning & Data Science journey with Complete Hands-on Learning & doubt solving Support
Click Here! (https://lastmomenttuitions.com/python-with-machine-learning/?ref=42057)

21. Consider the following assembly line problem:


time_to_reach[2][3] = {{17, 2, 7}, {19, 4, 9}}
time_spent[2][4] = {{6, 5, 15, 7}, {5, 10, 11, 4}}
entry_time[2] = {8, 10}
exit_time[2] = {10, 7}
num_of_stations = 4
What is the minimum time required to build the car chassis?
a) 40
b) 41
c) 42
d) 43
Answer: d
Explanation: The minimum time required is 43.
The path is S2,1 -> S1,2 -> S2,3 -> S2,4, where Si,j : i = line number, j = station number

22. Consider the following code:

#include<stdio.h>
int get_min(int a, int b)
{
if(a<b)
return a;
return b;
}
int minimum_time_required(int reach[][3],int spent[][4], int *entry, int *exit, int n)
{
int t1[n], t2[n],i;
t1[0] = entry[0] + spent[0][0];
t2[0] = entry[1] + spent[1][0];
for(i = 1; i < n; i++)
{
t1[i] = get_min(t1[i-1]+spent[0][i], t2[i-1]+reach[1][i-1]+spent[0][i]);
__________;
}
return get_min(t1[n-1]+exit[0], t2[n-1]+exit[1]);
}
Which of the following lines should be inserted to complete the above code?
a) t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i])
b) t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+spent[1][i])
c) t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1])
d) none of the mentioned
Answer: a
Explanation: The line t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i]) should be added to complete the above code.

23. What is the time complexity of the above dynamic programming implementation of the assembly line scheduling problem?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: b
Explanation: The time complexity of the above dynamic programming implementation of the assembly line scheduling problem is O(n).

24. What is the space complexity of the above dynamic programming implementation of the assembly line scheduling problem?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: b
Explanation: The space complexity of the above dynamic programming implementation of the assembly line scheduling problem is O(n).

25. What is the output of the following code?

#include<stdio.h>
int get_min(int a, int b)
{
if(a<b)
return a;
return b;
}
int minimum_time_required(int reach[][3],int spent[][4], int *entry, int *exit, int n)
{
int t1[n], t2[n], i;
t1[0] = entry[0] + spent[0][0];
t2[0] = entry[1] + spent[1][0];
for(i = 1; i < n; i++)
{
t1[i] = get_min(t1[i-1]+spent[0][i], t2[i-1]+reach[1][i-1]+spent[0][i]);
t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i]);
}
return get_min(t1[n-1]+exit[0], t2[n-1]+exit[1]);
}
int main()
{
int time_to_reach[][3] = {{6, 1, 5},
{2, 4, 7}};
int time_spent[][4] = {{6, 5, 4, 7},
{5, 10, 2, 6}};
int entry_time[2] = {5, 6};
int exit_time[2] = {8, 9};
int num_of_stations = 4;
int ans = minimum_time_required(time_to_reach, time_spent,
entry_time, exit_time, num_of_stations);
printf(“%d”,ans);
return 0;
}
a) 32
b) 33
c) 34
d) 35
Answer: c
Explanation: The program prints the optimal time required to build the car chassis, which is 34.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

26. What is the value stored in t1[2] when the following code is executed?
#include<stdio.h>
int get_min(int a, int b)
{
if(a<b)
return a;
return b;
}
int minimum_time_required(int reach[][3],int spent[][4], int *entry, int *exit, int n)
{
int t1[n], t2[n],i;
t1[0] = entry[0] + spent[0][0];
t2[0] = entry[1] + spent[1][0];
for(i = 1; i < n; i++)
{
t1[i] = get_min(t1[i-1]+spent[0][i], t2[i-1]+reach[1][i-1]+spent[0][i]);
t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i]);
}
return get_min(t1[n-1]+exit[0], t2[n-1]+exit[1]);
}
int main()
{
int time_to_reach[][3] = {{6, 1, 5},
{2, 4, 7}};
int time_spent[][4] = {{6, 5, 4, 7},
{5, 10, 2, 6}};
int entry_time[2] = {5, 6};
int exit_time[2] = {8, 9};
int num_of_stations = 4;
int ans = minimum_time_required(time_to_reach, time_spent,
entry_time, exit_time, num_of_stations);
printf(“%d”,ans);
return 0;
}
a) 16
b) 18
c) 20
d) 22
Answer: c
Explanation: The value stored in t1[2] when the above code is executed is 20.

27. What is the value stored in t2[3] when the following code is executed?
#include<stdio.h>
int get_min(int a, int b)
{
if(a<b)
return a;
return b;
}
int minimum_time_required(int reach[][3],int spent[][4], int *entry, int *exit, int n)
{
int t1[n], t2[n],i;
t1[0] = entry[0] + spent[0][0];
t2[0] = entry[1] + spent[1][0];
for(i = 1; i < n; i++)
{
t1[i] = get_min(t1[i-1]+spent[0][i], t2[i-1]+reach[1][i-1]+spent[0][i]);
t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i]);
}
return get_min(t1[n-1]+exit[0], t2[n-1]+exit[1]);
}
int main()
{
int time_to_reach[][3] = {{6, 1, 5},
{2, 4, 7}};
int time_spent[][4] = {{6, 5, 4, 7},
{5, 10, 2, 6}};
int entry_time[2] = {5, 6};
int exit_time[2] = {8, 9};
int num_of_stations = 4;
int ans = minimum_time_required(time_to_reach, time_spent,
entry_time, exit_time, num_of_stations);
printf(“%d”,ans);
return 0;
}
a) 19
b) 23
c) 25
d) 27
Answer: c
Explanation: The value stored in t2[3] when the above code is executed is 25.

28. What is the output of the following code?


#include<stdio.h>
int get_min(int a, int b)
{
if(a<b)
return a;
return b;
}
int minimum_time_required(int reach[][4],int spent[][5], int *entry, int *exit, int n)
{
int t1[n], t2[n], i;
t1[0] = entry[0] + spent[0][0];
t2[0] = entry[1] + spent[1][0];
for(i = 1; i < n; i++)
{
t1[i] = get_min(t1[i-1]+spent[0][i], t2[i-1]+reach[1][i-1]+spent[0][i]);
t2[i] = get_min(t2[i-1]+spent[1][i], t1[i-1]+reach[0][i-1]+spent[1][i]);
}
return get_min(t1[n-1]+exit[0], t2[n-1]+exit[1]);
}
int main()
{
int time_to_reach[][4] = {{16, 10, 5, 12},
{12, 4, 17, 8}};
int time_spent[][5] = {{13, 5, 20, 19, 9},
{15, 10, 12, 16, 13}};
int entry_time[2] = {12, 9};
int exit_time[2] = {10, 13};
int num_of_stations = 5;
int ans = minimum_time_required(time_to_reach, time_spent,
entry_time, exit_time, num_of_stations);
printf(“%d”,ans);
return 0;
}
a) 62
b) 69
c) 75
d) 88
Answer: d
Explanation: The program prints the optimal time required to build the car chassis, which is 88.

29. Floyd Warshall’s Algorithm is used for solving ____________


a) All pair shortest path problems
b) Single Source shortest path problems
c) Network flow problems
d) Sorting problems
Answer: a
Explanation: Floyd Warshall’s Algorithm is used for solving all pair shortest path problems. It means the algorithm is used for finding the shortest
paths between all pairs of vertices in a graph.

30. Floyd Warshall’s Algorithm can be applied on __________


a) Undirected and unweighted graphs
b) Undirected graphs
c) Directed graphs
d) Acyclic graphs
Answer: c
Explanation: Floyd Warshall Algorithm can be applied in directed graphs. From a given directed graph, an adjacency matrix is framed and then all
pair shortest path is computed by the Floyd Warshall Algorithm.

Python Programming for Complete Beginners

Start your Programming Journey with Python Programming which is Easy to Learn and Highly in Demand
Click Here! (https://lastmomenttuitions.com/complete-python-bootcamp/?ref=42057)

31. What is the running time of the Floyd Warshall Algorithm?


a) Big-oh(V)
b) Theta(V2)
c) Big-Oh(VE)
d) Theta(V3)
Answer: d
Explanation: The running time of the Floyd Warshall algorithm is determined by the triply nested for loops. Since each execution of the for loop
takes O(1) time, the algorithm runs in time Theta(V3).

32. What approach is being followed in Floyd Warshall Algorithm?


a) Greedy technique
b) Dynamic Programming
c) Linear Programming
d) Backtracking
Answer: b
Explanation: Floyd Warshall Algorithm follows dynamic programming approach because the all pair shortest paths are computed in bottom up
manner.

33. Floyd Warshall Algorithm can be used for finding _____________


a) Single source shortest path
b) Topological sort
c) Minimum spanning tree
d) Transitive closure
Answer: d
Explanation: One of the ways to compute the transitive closure of a graph in Theta(N3) time is to assign a weight of 1 to each edge of E and then
run the Floyd Warshall Algorithm.

34. What procedure is being followed in Floyd Warshall Algorithm?


a) Top down
b) Bottom up
c) Big bang
d) Sandwich
Answer: b
Explanation: Bottom up procedure is being used to compute the values of the matrix elements dij(k). The input is an n x n matrix. The procedure
returns the matrix D(n) of the shortest path weights.

35. Floyd- Warshall algorithm was proposed by ____________


a) Robert Floyd and Stephen Warshall
b) Stephen Floyd and Robert Warshall
c) Bernad Floyd and Robert Warshall
d) Robert Floyd and Bernad Warshall
Answer: a
Explanation: Floyd- Warshall Algorithm was proposed by Robert Floyd in the year 1962. The same algorithm was proposed by Stephen Warshall
during the same year for finding the transitive closure of the graph.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

36. Who proposed the modern formulation of Floyd-Warshall Algorithm as three nested loops?
a) Robert Floyd
b) Stephen Warshall
c) Bernard Roy
d) Peter Ingerman
Answer: d
Explanation: The modern formulation of Floyd-Warshall Algorithm as three nested for-loops was proposed by Peter Ingerman in the year 1962.

37. Complete the program.


n=rows[W]
D(0)=W
for k=1 to n
do for i=1 to n
do for j=1 to n
do ________________________________
return D(n)
a) dij(k)=min(dij(k-1), dik(k-1) – dkj(k-1))
b) dij(k)=max(dij(k-1), dik(k-1) – dkj(k-1))
c) dij(k)=min(dij(k-1), dik(k-1) + dkj(k-1))
d) dij(k)=max(dij(k-1), dik(k-1) + dkj(k-1))
Answer: c
Explanation: In order to compute the shortest path from vertex i to vertex j, we need to find the minimum of 2 values which are dij(k-1) and sum of
dik(k-1) and dkj(k-1).

38. What happens when the value of k is 0 in the Floyd Warshall Algorithm?
a) 1 intermediate vertex
b) 0 intermediate vertex
c) N intermediate vertices
d) N-1 intermediate vertices
Answer: b
Explanation: When k=0, a path from vertex i to vertex j has no intermediate vertices at all. Such a path has at most one edge and hence dij(0) = wij.
39. Using logical operator’s instead arithmetic operators saves time and space.
a) True
b) False
Answer: a
Explanation: In computers, logical operations on single bit values execute faster than arithmetic operations on integer words of data.

40. The time taken to compute the transitive closure of a graph is Theta(n2).
a) True
b) False
Answer: b
Explanation: The time taken to compute the transitive closure of a graph is Theta(n3). Transitive closure can be computed by assigning weight of 1
to each edge and by running Floyd Warshall Algorithm.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

41. In the given graph, what is the minimum cost to travel from vertex 1 to vertex 3?

a) 3
b) 2
c) 10
d) -3
Answer: d
Explanation: The minimum cost required to travel from node 1 to node 5 is -3.
1-5, cost is -4
5-4, cost is 6
4-3, cost is -5
Hence total cost is -4 + 6 + -5 = -3.

42. In the given graph, how many intermediate vertices are required to travel from node a to node e at a minimum cost?

a) 2
b) 0
c) 1
d) 3
Answer: c
Explanation: The minimum cost to travel from node a to node e is 1 by passing via nodes b and c.
a-b, cost 5
b-c, cost 3
c-e, cost -7
Hence the total cost is 1.

43. What is the formula to compute the transitive closure of a graph?


a) tij(k) = tij(k-1) AND (tik(k-1) OR tkj(k-1))
b) tij(k) = tij(k-1) OR (tik(k-1) AND tkj(k-1))
c) tij(k) = tij(k-1) AND (tik(k-1) AND tkj(k-1))
d) tij(k) = tij(k-1) OR (tik(k-1) OR tkj(k-1))
Answer: b
Explanation: Transitive closure of a graph can be computed by using Floyd Warshall algorithm. This method involves substitution of logical
operations (logical OR and logical AND) for arithmetic operations min and + in Floyd Warshall Algorithm.
Floyd Warshall Algorithm: dij(k)=min(dij(k-1), dik(k-1) + dkj(k-1))
Transitive closure: tij(k)= tij(k-1) OR (tik(k-1) AND tkj(k-1)).

42. Which of the following methods can be used to solve the longest common subsequence problem?
a) Recursion
b) Dynamic programming
c) Both recursion and dynamic programming
d) Greedy algorithm
Answer: c
Explanation: Both recursion and dynamic programming can be used to solve the longest subsequence problem.

43. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the longest common subsequence?
a) 9
b) 8
c) 7
d) 6
Answer: c
Explanation: The longest common subsequence is “PRTPQRS” and its length is 7.

44. Which of the following problems can be solved using the longest subsequence problem?
a) Longest increasing subsequence
b) Longest palindromic subsequence
c) Longest bitonic subsequence
d) Longest decreasing subsequence
Answer: b
Explanation: To find the longest palindromic subsequence in a given string, reverse the given string and then find the longest common subsequence
in the given string and the reversed string.

45. Longest common subsequence is an example of ____________


a) Greedy algorithm
b) 2D dynamic programming
c) 1D dynamic programming
d) Divide and conquer
Answer: b
Explanation: Longest common subsequence is an example of 2D dynamic programming.

Learn Machine Learning with Python from Scratch

Start your Machine learning & Data Science journey with Complete Hands-on Learning & doubt solving Support
Click Here! (https://lastmomenttuitions.com/python-with-machine-learning/?ref=42057)

46. What is the time complexity of the brute force algorithm used to find the longest common subsequence?
a) O(n)
b) O(n2)
c) O(n3)
d) O(2n)
Answer: d
Explanation: The time complexity of the brute force algorithm used to find the longest common subsequence is O(2n).

47. Consider the following dynamic programming implementation of the longest common subsequence problem:
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
______________;
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = ” abcedfg”, str2[] = “bcdfh”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
Which of the following lines completes the above code?
a) arr[i][j] = 1 + arr[i][j].
b) arr[i][j] = 1 + arr[i – 1][j – 1].
c) arr[i][j] = arr[i – 1][j – 1].
d) arr[i][j] = arr[i][j].
Answer: b
Explanation: The line, arr[i][j] = 1 + arr[i – 1][j – 1] completes the above code.

48. What is the time complexity of the following dynamic programming implementation of the longest common subsequence problem
where length of one string is “m” and the length of the other string is “n”?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = ” abcedfg”, str2[] = “bcdfh”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
a) O(n)
b) O(m)
c) O(m + n)
d) O(mn)
Answer: d
Explanation: The time complexity of the above dynamic programming implementation of the longest common subsequence is O(mn).

49. What is the space complexity of the following dynamic programming implementation of the longest common subsequence problem
where length of one string is “m” and the length of the other string is “n”?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = ” abcedfg”, str2[] = “bcdfh”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
a) O(n)
b) O(m)
c) O(m + n)
d) O(mn)
Answer: d
Explanation: The space complexity of the above dynamic programming implementation of the longest common subsequence is O(mn).

50. What is the output of the following code?


#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = “hbcfgmnapq”, str2[] = “cbhgrsfnmq”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
a) 3
b) 4
c) 5
d) 6
Answer: b
Explanation: The program prints the length of the longest common subsequence, which is 4.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

51. Which of the following is the longest common subsequence between the strings “hbcfgmnapq” and “cbhgrsfnmq” ?
a) hgmq
b) cfnq
c) bfmq
d) fgmna
Answer: d
Explanation: The length of the longest common subsequence is 4. But ‘fgmna’ is not the longest common subsequence as its length is 5.

52. What is the value stored in arr[2][3] when the following code is executed?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = “hbcfgmnapq”, str2[] = “cbhgrsfnmq”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
a) 1
b) 2
c) 3
d) 4
Answer: a
Explanation: The value stored in arr[2][3] is 1.

53. What is the output of the following code?


#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lcs(char *str1, char *str2)
{
int i,j,len1,len2;
len1 = strlen(str1);
len2 = strlen(str2);
int arr[len1 + 1][len2 + 1];
for(i = 0; i <= len1; i++)
arr[i][0] = 0;
for(i = 0; i <= len2; i++)
arr[0][i] = 0;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len1][len2];
}
int main()
{
char str1[] = “abcd”, str2[] = “efgh”;
int ans = lcs(str1,str2);
printf(“%d”,ans);
return 0;
}
a) 3
b) 2
c) 1
d) 0
Answer: d
Explanation: The program prints the length of the longest common subsequence, which is 0.

54. Which of the following methods can be used to solve the longest palindromic subsequence problem?
a) Dynamic programming
b) Recursion
c) Brute force
d) Dynamic programming, Recursion, Brute force
Answer: d
Explanation: Dynamic programming, Recursion, Brute force can be used to solve the longest palindromic subsequence problem.

55. Which of the following is not a palindromic subsequence of the string “ababcdabba”?
a) abcba
b) abba
c) abbbba
d) adba
Answer: d
Explanation: ‘adba’ is not a palindromic sequence.

55. For which of the following, the length of the string is not equal to the length of the longest palindromic subsequence?
a) A string that is a palindrome
b) A string of length one
c) A string that has all the same letters(e.g. aaaaaa)
d) Some strings of length two
Answer: d
Explanation: A string of length 2 for eg: ab is not a palindrome.
Python Programming for Complete Beginners

Start your Programming Journey with Python Programming which is Easy to Learn and Highly in Demand
Click Here! (https://lastmomenttuitions.com/complete-python-bootcamp/?ref=42057)

56. What is the length of the longest palindromic subsequence for the string “ababcdabba”?
a) 6
b) 7
c) 8
d) 9
Answer: b
Explanation: The longest palindromic subsequence is “abbabba” and its length is 7.

57. What is the time complexity of the brute force algorithm used to find the length of the longest palindromic subsequence?
a) O(1)
b) O(2n)
c) O(n)
d) O(n2)
Answer: b
Explanation: In the brute force algorithm, all the subsequences are found and the length of the longest palindromic subsequence is calculated. This
takes exponential time.

58. For every non-empty string, the length of the longest palindromic subsequence is at least one.
a) True
b) False
Answer: a
Explanation: A single character of any string can always be considered as a palindrome and its length is one.

59. Longest palindromic subsequence is an example of ______________


a) Greedy algorithm
b) 2D dynamic programming
c) 1D dynamic programming
d) Divide and conquer
Answer: b
Explanation: Longest palindromic subsequence is an example of 2D dynamic programming.

60. Consider the following code:


#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
______________;
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “ababcdabba”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
Which of the following lines completes the above code?
a) strrev(str2)
b) str2 = str1
c) len2 = strlen(str2)
d) strlen(str2)
Answer: a
Explanation: To find the longest palindromic subsequence, we need to reverse the copy of the string, which is done by strrev.

Crack Job Placement Aptitude in First Attempt

Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)

61. What is the time complexity of the following dynamic programming implementation to find the longest palindromic subsequence where
the length of the string is n?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “ababcdabba”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
a) O(n)
b) O(1)
c) O(n2)
d) O(2)
Answer: c
Explanation: The time complexity of the above dynamic programming implementation to find the longest palindromic subsequence is O(n2).

62. What is the space complexity of the following dynamic programming implementation to find the longest palindromic subsequence
where the length of the string is n?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “ababcdabba”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
a) O(n)
b) O(1)
c) O(n2)
d) O(2)
Answer: c
Explanation: The space complexity of the above dynamic programming implementation to find the longest palindromic subsequence is O(n2).

63. What is the value stored in arr[3][3] when the following code is executed?
#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “ababcdabba”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
a) 2
b) 3
c) 4
d) 5
Answer: a
Explanation: The value stored in arr[3][3] when the above code is executed is 2.

64. What is the output of the following code?


#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “abcd”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
Answer: b
Explanation: The program prints the length of the longest palindromic subsequence, which is 1.

65. What is the output of the following code?


#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[i][0] = 0;
for(i = 0; i <= len; i++)
arr[0][i] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j – 1])
arr[i][j] = 1 + arr[i – 1][j – 1];
else
arr[i][j] = max_num(arr[i – 1][j], arr[i][j – 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = “abdgkagdjbccbba”;
int ans = lps(str1);
printf(“%d”,ans);
return 0;
}
a) 5
b) 7
c) 9
d) 11
Answer: c
Explanation: The program prints the length of the longest palindromic subsequence, which is 9.

Learn Machine Learning with Python from Scratch

Start your Machine learning & Data Science journey with Complete Hands-on Learning & doubt solving Support
Click Here! (https://lastmomenttuitions.com/python-with-machine-learning/?ref=42057)

Backtracking and Branch and bound (#1617635582320-a3acc518-42aa)

String Matching Algorithms (#1617638004246-75673406-edcf)


Prepare For Your Placements: https://lastmomenttuitions.com/courses/placement-preparation/ (https://lastmomenttuitions.com/courses/placement-
preparation/)

(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-

included-mentorship/youtube-2/)

/ Youtube Channel: https://www.youtube.com/channel/UCGFNZxMqKLsqWERX_N2f08Q


(https://www.youtube.com/channel/UCGFNZxMqKLsqWERX_N2f08Q)

Follow For Latest Updates, Study Tips & More Content!

(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/insta-1/)/lastmomenttuition (https://www.instagram.com/lastmomenttuition/)

(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/link/)/ Last Moment Tuitions (https://in.linkedin.com/company/last-moment-
tuitions#:~:text=Last%20Moment%20Tuitions%20(LMT)%20is,others%20is%20its%20teaching%20methodology.)

(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/twittrwer/)/ lastmomentdost (https://twitter.com/lastmomentdost)

You might also like