Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the following is false about Prim’s algorithm?(a) It is a greedy algorithm(b) It constructs MST by selecting edges in increasing order of their weights(c) It never accepts cycles in the MST(d) It can be implemented using the Fibonacci heapThe question was posed to me in an interview for internship.I want to ask this question from Minimum Spanning Tree in chapter Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct choice is (b) It constructs MST by SELECTING edges in INCREASING order of their weights

For explanation: Prim’s algorithm can be implemented using Fibonacci heap and it NEVER accepts cycles. And Prim’s algorithm FOLLOWS greedy approach. Prim’s algorithms span from one vertex to another.

2.

Prim’s algorithm can be efficiently implemented using _____ for graphs with greater density.(a) d-ary heap(b) linear search(c) fibonacci heap(d) binary searchThis question was posed to me during an online exam.Query is from Minimum Spanning Tree topic in chapter Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct answer is (a) d-ary heap

Explanation: In Prim’s algorithm, we ADD the minimum weight edge for the chosen vertex which requires searching on the ARRAY of WEIGHTS. This searching can be efficiently IMPLEMENTED using binary heap for dense GRAPHS. And for graphs with greater density, Prim’s algorithm can be made to run in linear time using d-ary heap(generalization of binary heap).

3.

Prim’s algorithm is also known as __________(a) Dijkstra–Scholten algorithm(b) Borůvka’s algorithm(c) Floyd–Warshall algorithm(d) DJP AlgorithmThe question was posed to me during an interview for a job.My doubt stems from Minimum Spanning Tree in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct option is (d) DJP ALGORITHM

Easiest explanation - The Prim’s algorithm was developed byVojtěch Jarník and it was LATTER DISCOVERED by the duo Prim and DIJKSTRA. Therefore, Prim’s algorithm is also known as DJP Algorithm.

4.

Kruskal’s algorithm is best suited for the sparse graphs than the prim’s algorithm.(a) True(b) FalseThe question was posed to me at a job interview.Asked question is from Minimum Spanning Tree topic in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer» CORRECT choice is (a) True

For EXPLANATION: Prim’s algorithm and Kruskal’s algorithm perform equally in case of the sparse graphs. But Kruskal’s algorithm is simpler and EASY to work with. So, it is BEST suited for sparse graphs.
5.

Prim’s algorithm resemblesDijkstra’s algorithm.(a) True(b) FalseI had been asked this question in quiz.Question is from Minimum Spanning Tree in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer» CORRECT answer is (a) True

Best EXPLANATION: In Prim’s ALGORITHM, the MST is CONSTRUCTED starting from a single vertex and adding in new edges to the MST that link the partial tree to a new vertex outside of the MST. And Dijkstra’s algorithm also rely on the SIMILAR approach of finding the next closest vertex. So, Prim’s algorithm resembles Dijkstra’s algorithm.
6.

Prim’s algorithm is a ______(a) Divide and conquer algorithm(b) Greedy algorithm(c) Dynamic Programming(d) Approximation algorithmI got this question in class test.My question is taken from Minimum Spanning Tree in chapter Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct CHOICE is (b) Greedy algorithm

Easiest explanation - Prim’s algorithm uses a greedy algorithm APPROACH to find the MST of the connected WEIGHTED graph. In greedy method, we ATTEMPT to find an optimal solution in STAGES.

7.

Worst case is the worst case time complexity of Prim’s algorithm if adjacency matrix is used?(a) O(log V)(b) O(V^2)(c) O(E^2)(d) O(V log E)I had been asked this question during an interview.Query is from Minimum Spanning Tree topic in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right answer is (b) O(V^2)

The explanation is: Use of adjacency matrix provides the SIMPLE IMPLEMENTATION of the Prim’s algorithm. In Prim’s algorithm, we need to SEARCH for the EDGE with a minimum for that VERTEX. So, worst case time complexity will be O(V^2), where V is the number of vertices.

8.

Which of the following is true?(a) Prim’s algorithm initialises with a vertex(b) Prim’s algorithm initialises with a edge(c) Prim’s algorithm initialises with a vertex which has smallest edge(d) Prim’s algorithm initialises with a forestThis question was addressed to me in examination.Origin of the question is Minimum Spanning Tree in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right ANSWER is (a) Prim’s algorithm initialises with a VERTEX

The explanation is: Steps in Prim’s algorithm: (I) Select any vertex of GIVEN graph and add it to MST (II) Add the edge of minimum WEIGHT from a vertex not in MST to the vertex in MST; (III) It MST is complete the stop, otherwise go to step (II).

9.

Kruskal’s algorithm is best suited for the dense graphs than the prim’s algorithm.(a) True(b) FalseI have been asked this question by my college professor while I was bunking the class.Question is from Minimum Spanning Tree in section Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct CHOICE is (B) False

Easiest EXPLANATION - Prim’s algorithm outperforms the Kruskal’s algorithm in case of the DENSE graphs. It is significantly FASTER if graph has more edges than the Kruskal’s algorithm.

10.

Which of the following is false about the Kruskal’s algorithm?(a) It is a greedy algorithm(b) It constructs MST by selecting edges in increasing order of their weights(c) It can accept cycles in the MST(d) It uses union-find data structureThe question was posed to me in unit test.Question is from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right answer is (C) It can accept cycles in the MST

For explanation: KRUSKAL’s algorithm is a greedy algorithm to construct the MST of the given graph. It constructs the MST by selecting EDGES in increasing ORDER of their weights and rejects an edge if it may form the cycle. So, using Kruskal’s algorithm is never formed.

11.

Which of the following is true?(a) Prim’s algorithm can also be used for disconnected graphs(b) Kruskal’s algorithm can also run on the disconnected graphs(c) Prim’s algorithm is simpler than Kruskal’s algorithm(d) In Kruskal’s sort edges are added to MST in decreasing order of their weightsI got this question in exam.I want to ask this question from Minimum Spanning Tree in section Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct OPTION is (b) Kruskal’s algorithm can also run on the DISCONNECTED GRAPHS

Easiest EXPLANATION - Prim’s algorithm iterates from ONE node to another, so it can not be applied for disconnected graph. Kruskal’s algorithm can be applied to the disconnected graphs to construct the minimum cost forest. Kruskal’s algorithm is comparatively easier and simpler than prim’s algorithm.

12.

Which of the following edges form minimum spanning tree on the graph using kruskals algorithm?(a) (B-E)(G-E)(E-F)(D-F)(b) (B-E)(G-E)(E-F)(B-G)(D-F)(c) (B-E)(G-E)(E-F)(D-E)(d) (B-E)(G-E)(E-F)(D-F)(D-G)The question was posed to me in unit test.My enquiry is from Minimum Spanning Tree topic in chapter Minimum Spanning Tree of Data Structures & Algorithms II

Answer» CORRECT ANSWER is (a) (B-E)(G-E)(E-F)(D-F)

For explanation: Using Krushkal’s algorithm on the given graph, the generated minimum spanning TREE is SHOWN below.

So, the edges in the MST are, (B-E)(G-E)(E-F)(D-F).
13.

Consider the following graph. Using Kruskal’s algorithm, which edge will be selected first?(a) GF(b) DE(c) BE(d) BGThis question was addressed to me in an internship interview.This intriguing question comes from Minimum Spanning Tree topic in section Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct OPTION is (c) BE

Explanation: In Krsuskal’s algorithm the edges are selected and added to the SPANNING tree in increasing ORDER of their weights. THEREFORE, the first EDGE selected will be the minimal one. So, correct option is BE.

14.

What is the time complexity of Kruskal’s algorithm?(a) O(log V)(b) O(E log V)(c) O(E^2)(d) O(V log E)This question was addressed to me in a national level competition.This key question is from Minimum Spanning Tree in section Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct answer is (b) O(E LOG V)

The explanation is: Kruskal’s algorithm involves SORTING of the edges, which takes O(E LOGE) TIME, where E is a number of edges in graph and V is the number of vertices. After sorting, all edges are iterated and union-find algorithm is applied. union-find algorithm requires O(logV) time. So, overall Kruskal’s algorithm requires O(E log V) time.

15.

Kruskal’s algorithm is a ______(a) divide and conquer algorithm(b) dynamic programming algorithm(c) greedy algorithm(d) approximation algorithmThis question was posed to me in an online quiz.I want to ask this question from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right OPTION is (c) greedy algorithm

To explain: KRUSKAL’s algorithm USES a greedy algorithm approach to find the MST of the connected weighted GRAPH. In the greedy method, we attempt to find an optimal solution in stages.

16.

Kruskal’s algorithm is used to ______(a) find minimum spanning tree(b) findsingle source shortest path(c) find all pair shortest path algorithm(d) traverse the graphThe question was posed to me by my college director while I was bunking the class.This interesting question is from Minimum Spanning Tree in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct answer is (a) find minimum SPANNING tree

The best I can EXPLAIN: TheKruskal’s algorithm is used to find the minimum spanning tree of the connected graph. It construct the MST by finding the edge having the least possible weight that connects two trees in the FOREST.

17.

Which of the following is false?(a) The spanning trees do not have any cycles(b) MST have n – 1 edges if the graph has n edges(c) Edge e belonging to a cut of the graph if has the weight smaller than any other edge in the same cut, then the edge e is present in all the MSTs of the graph(d) Removing one edge from the spanning tree will not make the graph disconnectedThe question was posed to me in exam.My question is based upon Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct answer is (d) Removing one edge from the spanning tree will not make the graph disconnected

The best EXPLANATION: Every spanning tree has n – 1 EDGES if the graph has n edges and has no cycles. The MST follows the cut PROPERTY, Edge e BELONGING to a cut of the graph if has the weight smaller than any other edge in the same cut, then the edge e is present in all the MSTS of the graph.

18.

Which of the following is not the algorithm to find the minimum spanning tree of the given graph?(a) Boruvka’s algorithm(b) Prim’s algorithm(c) Kruskal’s algorithm(d) Bellman–Ford algorithmI had been asked this question in final exam.My question comes from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right answer is (d) Bellman–Ford algorithm

Easiest explanation - The BORUVKA’s algorithm, PRIM’s algorithm and Kruskal’s algorithm are the algorithms that can be used to find the minimum spanning TREE of the given graph.

The Bellman-Ford algorithm is used to find the shortest path from the SINGLE SOURCE to all other vertices.

19.

Consider the graph shown below. Which of the following are the edges in the MST of the given graph?(a) (a-c)(c-d)(d-b)(d-b)(b) (c-a)(a-d)(d-b)(d-e)(c) (a-d)(d-c)(d-b)(d-e)(d) (c-a)(a-d)(d-c)(d-b)(d-e)The question was posed to me during a job interview.This question is from Minimum Spanning Tree in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The CORRECT option is (c) (a-d)(d-c)(d-b)(d-e)

Explanation: The minimum SPANNING TREE of the given GRAPH is shown below. It has cost 56.

20.

If all the weights of the graph are positive, then the minimum spanning tree of the graph is a minimum cost subgraph.(a) True(b) FalseThe question was posed to me in an online interview.My query is from Minimum Spanning Tree in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct answer is (a) True

The BEST explanation: A subgraph is a GRAPH FORMED from a subset of the vertices and edges of the ORIGINAL graph. And the subset of vertices includes all endpoints of the subset of the edges. So, we can say MST of a graph is a subgraph when all weights in the original graph are positive.

21.

Consider a undirected graph G with vertices { A, B, C, D, E}. In graph G, every edge has distinct weight. Edge CD is edge with minimum weight and edge AB is edge with maximum weight. Then, which of the following is false?(a) Every minimum spanning tree of G must contain CD(b) If AB is in a minimum spanning tree, then its removal must disconnect G(c) No minimum spanning tree contains AB(d) G has a unique minimum spanning treeI have been asked this question in an international level competition.Question is taken from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right option is (c) No minimum spanning tree contains AB

Easiest explanation - Every MST will contain CD as it is SMALLEST EDGE. So, Every minimum spanning tree of G must contain CD is true. And G has a unique minimum spanning tree is also true because the graph has EDGES with distinct weights. So, no minimum spanning tree contains AB is false.

22.

Consider the graph M with 3 vertices. Its adjacency matrix is shown below. Which of the following is true?(a) Graph M has no minimum spanning tree(b) Graph M has a unique minimum spanning trees of cost 2(c) Graph M has 3 distinct minimum spanning trees, each of cost 2(d) Graph M has 3 spanning trees of different costsThe question was asked in an interview for internship.My question comes from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The CORRECT option is (c) Graph M has 3 DISTINCT MINIMUM spanning trees, each of cost 2

The best explanation: Here all non-diagonal elements in the adjacency MATRIX are 1. So, EVERY vertex is connected every other vertex of the graph. And, so graph M has 3 distinct minimum spanning trees.

23.

The travelling salesman problem can be solved using _________(a) A spanning tree(b) A minimum spanning tree(c) Bellman – Ford algorithm(d) DFS traversalI have been asked this question in class test.The query is from Minimum Spanning Tree topic in division Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The correct choice is (b) A minimum spanning TREE

To explain: In the travelling salesman problem we have to find the shortest possible ROUTE that visits every CITY exactly once and returns to the starting point for the given a set of cities. So, travelling salesman problem can be solved by CONTRACTING the minimum spanning tree.

24.

Consider a complete graph G with 4 vertices. The graph G has ____ spanning trees.(a) 15(b) 8(c) 16(d) 13I got this question during an interview for a job.The query is from Minimum Spanning Tree in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Right option is (c) 16

Easy explanation - A GRAPH can have many spanning TREES. And a complete graph with N vertices has n^(n-2) spanning trees. So, the complete graph with 4 vertices has 4^(4-2)= 16 spanning trees.

25.

Every graph has only one minimum spanning tree.(a) True(b) FalseI have been asked this question in an interview for job.My question is based upon Minimum Spanning Tree topic in portion Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

The CORRECT choice is (b) False

For EXPLANATION: Minimum spanning tree is a spanning tree with the lowest cost AMONG all the spacing trees. Sum of all of the edges in the spanning tree is the cost of the spanning tree. There can be many minimum spanning trees for a GIVEN graph.

26.

Which of the following is false in the case of a spanning tree of a graph G?(a) It is tree that spans G(b) It is a subgraph of the G(c) It includes every vertex of the G(d) It can be either cyclic or acyclicThis question was addressed to me in final exam.My query is from Minimum Spanning Tree in chapter Minimum Spanning Tree of Data Structures & Algorithms II

Answer»

Correct option is (d) It can be either cyclic or acyclic

Explanation: A GRAPH can have many spanning trees. Each spanning TREE of a graph G is a subgraph of the graph G, and spanning trees include every VERTEX of the GRAM. Spanning trees are always acyclic.