0% found this document useful (0 votes)
23 views24 pages

MCQ

The document consists of multiple-choice questions (MCQs) covering various algorithms and data structures, including the Bellman-Ford algorithm, Breadth-First Search (BFS), Depth-First Search (DFS), Dial's Algorithm, Heap Sort, K-ary Heaps, and binary tree traversal techniques. Each question provides options with the correct answer indicated, focusing on time complexity, data structures used, and specific characteristics of the algorithms. The content serves as a study guide for understanding fundamental concepts in computer science related to graph algorithms and tree data structures.

Uploaded by

ereneyeager9997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views24 pages

MCQ

The document consists of multiple-choice questions (MCQs) covering various algorithms and data structures, including the Bellman-Ford algorithm, Breadth-First Search (BFS), Depth-First Search (DFS), Dial's Algorithm, Heap Sort, K-ary Heaps, and binary tree traversal techniques. Each question provides options with the correct answer indicated, focusing on time complexity, data structures used, and specific characteristics of the algorithms. The content serves as a study guide for understanding fundamental concepts in computer science related to graph algorithms and tree data structures.

Uploaded by

ereneyeager9997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Question 1:

The Bellman-Ford algorithm is used to find the shortest paths from a single source vertex in a
weighted graph. Which of the following is a key feature of this algorithm?

a) It can only handle graphs with positive edge weights.

b) It can detect negative weight cycles.

c) It has a time complexity of O(V log V).

d) It uses a priority queue.

Answer: b) It can detect negative weight cycles.

Question 2:

What is the time complexity of the Bellman-Ford algorithm for a graph with V vertices and E
edges?

a) O(V + E)

b) O(V log V)

c) O(E log V)

d) O(V * E)

Answer: d) O(V * E)

Question 3:

How many iterations does the Bellman-Ford algorithm perform to find the shortest paths?

a) V iterations

b) V - 1 iterations

c) E iterations

d) E - 1 iterations
Answer: b) V - 1 iterations

Question 4:

If the Bellman-Ford algorithm detects a negative weight cycle, what does this indicate?

a) There are no paths from the source to some vertices.

b) There is an error in the input graph.

c) The shortest paths are undefined due to the cycle.

d) The graph is disconnected.

Answer: c) The shortest paths are undefined due to the cycle.

Question 5:

Which of the following scenarios is the Bellman-Ford algorithm best suited for?

a) Graphs with only positive edge weights.

b) Graphs with a large number of vertices and edges.

c) Graphs with negative edge weights or the possibility of negative weight cycles.

d) Graphs where the shortest path needs to be found very quickly.

Answer: c) Graphs with negative edge weights or the possibility of negative weight cycles.

Question 1:

What data structure is primarily used in the Breadth-First Search (BFS) algorithm?

a) Stack

b) Queue

c) Priority Queue
d) Linked List

Answer: b) Queue

Question 2:

Which of the following describes the order in which BFS visits nodes in a graph?

a) Depth-first

b) Level-by-level

c) Randomly

d) Inorder

Answer: b) Level-by-level

Question 3:

What is the time complexity of the Breadth-First Search (BFS) algorithm for a graph represented
as an adjacency list?

a) O(V log V)

b) O(E log V)

c) O(V + E)

d) O(V * E)

Answer: c) O(V + E)

Question 4:

If a graph is disconnected and you run BFS from a starting node, what will happen?
a) BFS will visit all nodes in the graph.

b) BFS will only visit the nodes in the connected component of the starting node.

c) BFS will throw an exception.

d) BFS will enter an infinite loop.

Answer: b) BFS will only visit the nodes in the connected component of the starting node.

Question 5:

Which of the following is a common application of Breadth-First Search (BFS)?

a) Finding the shortest path in a weighted graph.

b) Detecting cycles in a directed graph.

c) Finding the shortest path in an unweighted graph.

d) Sorting a list of elements.

Answer: c) Finding the shortest path in an unweighted graph.

MCQ Questions:

Question 1:

What data structure forms the basis of a Binomial Heap?

a) Binary Search Trees

b) Linked Lists

c) Binomial Trees

d) Arrays

Answer: c) Binomial Trees

Question 2:
What is the time complexity of the extractMin() operation in a Binomial Heap?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

Question 3:

How many binomial trees of a specific degree can exist in a Binomial Heap?

a) Any number

b) At most one

c) Exactly two

d) Depends on the input

Answer: b) At most one

Question 4:

Which operation is used to combine two Binomial Heaps?

a) Merge Sort

b) Union

c) Heapify

d) Reverse List

Answer: b) Union
Question 5:

What is the degree of a binomial tree that has 8 nodes?

a) 2

b) 3

c) 4

d) 8

Answer: b) 3 (2^3 = 8)

Question 1:

What are the components of a boundary traversal of a binary tree?

a) Root, left boundary, right boundary

b) Root, left boundary, leaf nodes, right boundary

c) Left boundary, leaf nodes, right boundary

d) Root, leaf nodes

Answer: b) Root, left boundary, leaf nodes, right boundary

Question 2:

In which order is the right boundary printed in a boundary traversal?

a) Top to bottom

b) Bottom to top

c) Level order

d) Inorder
Answer: b) Bottom to top

Question 3:

Which nodes are excluded from the left and right boundary during a boundary traversal?

a) Root node

b) Leaf nodes

c) Internal nodes

d) All nodes

Answer: b) Leaf nodes

Question 4:

What is the time complexity of the boundary traversal algorithm?

a) O(log n)

b) O(n)

c) O(n log n)

d) O(n^2)

Answer: b) O(n)

Question 5:

If a left subtree is missing but a right subtree exists, which node is included in the left boundary?

a) The root node

b) The right child node

c) The left child node


d) No node is included

Answer: b) The right child node

Question 1:

What data structure is implicitly used in the recursive implementation of Depth-First Search
(DFS)?

a) Queue

b) Stack

c) Priority Queue

d) Linked List

Answer: b) Stack (implicitly through the call stack)

Question 2:

Which of the following describes the order in which DFS visits nodes in a graph?

a) Level-by-level

b) Depth-first

c) Randomly

d) Breadth-first

Answer: b) Depth-first

Question 3:

What is the time complexity of the Depth-First Search (DFS) algorithm for a graph represented
as an adjacency list?

a) O(V log V)
b) O(E log V)

c) O(V + E)

d) O(V * E)

Answer: c) O(V + E)

Question 4:

If a graph contains a cycle, what might happen during a DFS traversal?

a) DFS will terminate normally.

b) DFS might enter an infinite loop (if not handled properly).

c) DFS will throw an exception.

d) DFS will always find the shortest path.

Answer: b) DFS might enter an infinite loop (if not handled properly).

Question 5:

Which of the following is a common application of Depth-First Search (DFS)?

a) Finding the shortest path in an unweighted graph.

b) Detecting cycles in a directed graph.

c) Finding the shortest path in a weighted graph.

d) Sorting a list of elements.

Answer: b) Detecting cycles in a directed graph.

Question 1:

Dial's Algorithm is most efficient for graphs with:


a) Negative edge weights.

b) Positive real-valued edge weights.

c) Positive integer edge weights within a limited range.

d) Unweighted edges.

Answer: c) Positive integer edge weights within a limited range.

Question 2:

What data structure does Dial's Algorithm primarily use to organize vertices based on their
distances?

a) Priority Queue

b) Stack

c) Buckets (an array of queues)

d) Linked List

Answer: c) Buckets (an array of queues)

Question 3:

Dial's Algorithm is a variation of which other shortest-path algorithm?

a) Bellman-Ford Algorithm

b) Floyd-Warshall Algorithm

c) Dijkstra's Algorithm

d) Breadth-First Search (BFS)

Answer: c) Dijkstra's Algorithm

Question 4:
The time complexity of Dial's Algorithm depends on:

a) The number of vertices only.

b) The number of edges only.

c) The maximum edge weight and the number of vertices.

d) The number of vertices and edges, but not the edge weights.

Answer: c) The maximum edge weight and the number of vertices.

Question 5:

Dial's Algorithm is particularly useful when:

a) The graph has negative cycles.

b) The graph is very sparse.

c) The edge weights are large and varied.

d) The edge weights are small integers and the graph is dense.

Answer: d) The edge weights are small integers and the graph is dense.

MCQ Questions:

Question 1:

What is the time complexity of the Heap Sort algorithm?

a) O(n)

b) O(n log n)

c) O(n^2)

d) O(log n)
Answer: b) O(n log n)

Question 2:

Which data structure is used as the basis for Heap Sort?

a) Queue

b) Stack

c) Heap

d) Linked List

Answer: c) Heap

Question 3:

Heap Sort is an example of a(n):

a) Stable sorting algorithm

b) Unstable sorting algorithm

c) Adaptive sorting algorithm

d) Divide and conquer algorithm

Answer: b) Unstable sorting algorithm

Question 4:

In Heap Sort, the initial array is converted into a:

a) Min-heap

b) Max-heap

c) Binary search tree


d) Sorted array

Answer: b) Max-heap

Question 5:

What is the space complexity of Heap Sort?

a) O(1) (in-place)

b) O(n)

c) O(log n)

d) O(n log n)

Answer: a) O(1) (in-place)

MCQ Questions:

Question 1:

In a K-ary Heap, how many children does each non-leaf node have?

a) 2

b) K

c) Variable

d) Depends on the input

Answer: b) K

Question 2:

What is the time complexity of the extractMin() operation in a K-ary Heap?


a) O(log n)

b) O(k log n)

c) O(n log k)

d) O(n log n)

Answer: b) O(k log n)

Question 3:

How is the parent of a node at index i calculated in a K-ary Heap?

a) (i - 1) / 2

b) (i - 1) / K

c) (i + 1) / K

d) i / K

Answer: b) (i - 1) / K

Question 4:

What is the primary advantage of a K-ary Heap compared to a Binary Heap (K=2)?

a) Faster insertion

b) Faster extraction of the minimum

c) Better cache locality for larger K

d) Smaller memory footprint

Answer: c) Better cache locality for larger K

Question 5:
If a K-ary Heap is used as a priority queue, which operation is typically the most frequently
used?

a) Insert

b) ExtractMin

c) DecreaseKey

d) IncreaseKey

Answer: b) ExtractMin

Question 1:

What is the primary goal of the "Recover Binary Search Tree" problem?

a) To balance the BST.

b) To delete nodes from the BST.

c) To correct the positions of two swapped nodes in a BST.

d) To insert new nodes into the BST.

Answer: c) To correct the positions of two swapped nodes in a BST.

Question 2:

Which traversal method is most commonly used to identify the swapped nodes in a BST?

a) Preorder traversal.

b) Postorder traversal.

c) Inorder traversal.

d) Level order traversal.

Answer: c) Inorder traversal.


Question 3:

If an inorder traversal of a BST results in the sequence [1, 5, 3, 4, 2, 6], which two nodes were
swapped?

a) 1 and 2.

b) 3 and 2.

c) 5 and 4.

d) 5 and 2.

Answer: d) 5 and 2.

Question 4:

What is the time complexity of the most efficient solution to recover a BST?

a) O(n^2).

b) O(log n).

c) O(n).

d) O(n log n).

Answer: c) O(n).

Question 5:

In the "Recover BST" problem, what indicates that two nodes are swapped in an inorder
traversal?

a) A node's value is greater than the previous node's value.

b) A node's value is less than the previous node's value.

c) Two consecutive nodes have the same value.

d) A null node is encountered.


Answer: b) A node's value is less than the previous node's value.

Question 1:

Topological sort is applicable to which type of graph?

a) Undirected graphs

b) Directed graphs with cycles

c) Directed acyclic graphs (DAGs)

d) Weighted graphs

Answer: c) Directed acyclic graphs (DAGs)

Question 2:

What is the primary purpose of topological sort?

a) To find the shortest path in a graph.

b) To detect cycles in a graph.

c) To order vertices such that for every directed edge (u, v), vertex u comes before vertex v.

d) To find the minimum spanning tree of a graph.

Answer: c) To order vertices such that for every directed edge (u, v), vertex u comes before vertex
v.

Question 3:

Which data structure is typically used in Kahn's algorithm for topological sort?

a) Stack

b) Queue
c) Priority Queue

d) Linked List

Answer: b) Queue

Question 4:

If a directed graph contains a cycle, what will happen when attempting to perform a topological
sort?

a) The algorithm will produce a valid topological order.

b) The algorithm will terminate normally but not produce a valid topological order.

c) The algorithm will detect the cycle and return an error or null.

d) The algorithm will enter an infinite loop.

Answer: c) The algorithm will detect the cycle and return an error or null.

Question 5:

What is the time complexity of Kahn's algorithm for topological sort, given a graph with V
vertices and E edges?

a) O(V + E)

b) O(V log V)

c) O(E log V)

d) O(V * E)

Answer: a) O(V + E)

Question 1:

What data structure is used to store nodes based on their horizontal distance during vertical
order traversal?
a) ArrayList

b) HashMap

c) TreeMap

d) HashSet

Answer: c) TreeMap (to maintain sorted order)

Question 2:

Which traversal method is used as the basis for vertical order traversal?

a) Preorder traversal

b) Inorder traversal

c) Level order traversal (BFS)

d) Postorder traversal

Answer: c) Level order traversal (BFS)

Question 3:

What does the horizontal distance (HD) represent in the context of vertical order traversal?

a) The depth of the node

b) The level of the node

c) The position of the node relative to the root in the horizontal direction

d) The number of children a node has

Answer: c) The position of the node relative to the root in the horizontal direction

Question 4:
What is the time complexity of the vertical order traversal algorithm?

a) O(n)

b) O(n log n)

c) O(n^2)

d) O(log n)

Answer: b) O(n log n) (due to the TreeMap)

Question 5:

If two nodes have the same horizontal distance, how are they ordered in the vertical order
traversal output?

a) By their level (top to bottom)

b) By their value (ascending order)

c) By their right-to-left order

d) By their parent node's value

Answer: a) By their level (top to bottom)

MCQ Questions:

Question 1:

Which traversal method is used as the basis for the left and right view algorithms?

a) Preorder traversal

b) Inorder traversal

c) Level order traversal (BFS)

d) Postorder traversal
Answer: c) Level order traversal (BFS)

Question 2:

What is the primary data structure used in the BFS-based tree view algorithms?

a) Stack

b) Queue

c) Priority Queue

d) Linked List

Answer: b) Queue

Question 3:

In the top view algorithm, what data structure is used to maintain the horizontal distances?

a) ArrayList

b) HashSet

c) TreeMap

d) HashMap

Answer: c) TreeMap

Question 4:

Which node is added to the result in the left view algorithm during each level traversal?

a) The last node in the level

b) The first node in the level


c) All nodes in the level

d) Only the root node

Answer: b) The first node in the level

Question 5:

What is the primary purpose of the Pair class used in the top view algorithm?

a) To store the node's value and its level

b) To store the node's value and its horizontal distance

c) To store the node's left and right children

d) To store the node's parent and its value

Answer: b) To store the node's value and its horizontal distance

MCQ Questions:

Question 1:

What is the primary purpose of a Winner Tree?

a) Sorting an array

b) Merging sorted lists efficiently

c) Searching for an element in a tree

d) Finding the shortest path in a graph

Answer: b) Merging sorted lists efficiently

Question 2:

A Winner Tree is a type of:


a) Binary Search Tree

b) Heap

c) Complete Binary Tree

d) Graph

Answer: c) Complete Binary Tree

Question 3:

In a Winner Tree, the root node stores:

a) The largest element of the input.

b) The smallest element of the input.

c) The median of the input.

d) The average of the input.

Answer: b) The smallest element of the input.

Question 4:

The height of a Winner Tree is approximately:

a) O(n)

b) O(log n)

c) O(n^2)

d) O(1)

Answer: b) O(log n)

Question 5:
When an element in the input array is updated, how is the Winner Tree updated?

a) Only the root node is updated.

b) Only the leaf node corresponding to the updated element is updated.

c) The path from the updated leaf node to the root is updated.

d) The entire Winner Tree is rebuilt.

Answer: c) The path from the updated leaf node to the root is updated.

You might also like