Advanced Ds Mcqs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

ST II

ST - III
BINARY TREES

1. What is the maximum number of nodes in a binary tree with height h?

a) 2^h-1

b) 2^h

c) 2^(h+1)

d) 2^(h-1)

Answer: a) 2^h-1

2. What type of traversal visits the left sub-tree, the root, and then the right sub-tree of a binary tree?

a) In-order

b) Pre-order

c) Post-order

d) Level-order

Answer: a) In-order

3.What is the time complexity of searching for a node in a binary search tree?

a) O(n)

b) O(log n)

c) O(1)

d) O(n log n)

Answer: b) O(log n)

4.What is the technique used to balance a binary search tree?

a) Breadth-first search

b) Depth-first search

c) AVL rotation

d) Hash table

Answer: c) AVL rotation


5. How is a binary tree represented in memory?

a) As an array

b) As a linked list

c) As a hash table

d) As a graph

Answer: b) As a linked list


Binary Search Trees: Introduction to Binary Search Trees,
Traversals ( recursive& non recursive), Delete a Node, Find
Height of a tree, Path in a tree, Level Order Traversal, Top View
and Bottom View of a tree, Lowest Common Ancestor

1. What is the property of a binary search tree that the left child of a node must have a value smaller
than that of the node, and the right child must have a value greater than that of the node?
a) Sorted order
b) Complete tree
c) Balanced tree
d) Search property
Answer: d) Search Property

2. What is the time complexity of finding the minimum element in a binary search tree?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: a) O(1)

3. Which traversal visits the left sub-tree, the root, and then the right sub-tree of a binary search tree?
a) In-order
b) Pre-order
c) Post-order
d) Level-order
Answer: a) In-order
4. What is the technique used to delete a node from a binary search tree?
a) Breadth-first search
b) Depth-first search
c) AVL rotation
d) Node replacement
Answer: d) Node Replacement

5. What is the method used to find the lowest common ancestor of two nodes in a binary search tree?
a) Breadth-first search
b) Depth-first search
c) Backtracking
d) Recursion
Answer: d) Recursion

6. How is the top view and bottom view of a tree represented?


a) As an array
b) As a linked list
c) As a hash table
d) As a graph
Answer: d) As a graph

7. What is the time complexity of searching for a node in a balanced binary search tree?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: b) O(log n)
8. What is the time complexity of inserting a node in a balanced binary search tree?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: b) O(log n)

9. What is the time complexity of deleting a node in a balanced binary search tree?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: O(log n)

10. What is the property that must be maintained in a self-balancing binary search tree to ensure that
the tree remains balanced?
a) Sorted order
b) Complete tree
c) Balanced tree
d) Search property
Answer: c) Balanced tree

11.What is the algorithm used to balance a binary search tree when a node is inserted or deleted?
a) Breadth-first search
b) Depth-first search
c) AVL rotation
d) Hash table
Answer: c) AVL rotation
12. How is the height of a binary search tree calculated?
a) Number of nodes
b) Maximum depth
c) Minimum depth
d) Average depth
Answer: b) Maximum depth
AVL Trees: Balanced Trees, Introduction to the AVL trees,
Implementation of AVL Trees

1. What is the name of the algorithm that is used to maintain balance in AVL trees?

a) Breadth-first search

b) Depth-first search

c) AVL rotation

d) Hash table

Answer: c) AVL rotation

2. What is the minimum height of an AVL tree with n nodes?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

3. What is the maximum number of rotations required to insert or delete a node in an AVL tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

4. What is the property that must be maintained in an AVL tree to ensure that the tree remains
balanced?

a) Sorted order

b) Complete tree

c) Height-balanced
d) Search property

Answer: c) Height-balanced

5. What is the difference between the height of the left subtree and the height of the right subtree in
an AVL tree called?

a) Balance factor

b) Height factor

c) Weight factor

d) Size factor

Answer: a) Balance factor

6. How is the height of an AVL tree calculated?

a) Number of nodes

b) Maximum depth

c) Minimum depth

d) Average depth

Answer: b) Maximum depth

7. What are the two types of rotations used in AVL trees to maintain balance?

a) Left rotation and Right rotation

b) Single rotation and Double rotation

c) In-order rotation and Post-order rotation

d) Pre-order rotation and Level-order rotation

Answer: a) Left rotation and Right rotation

8. What is the time complexity of searching for a node in an AVL tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)
9. What is the time complexity of inserting a node in an AVL tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

10. What is the time complexity of deleting a node in an AVL tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

11. What is the maximum difference in height between the left and right subtrees in an AVL tree?

a) 1

b) 2

c) log n

d) n

Answer: a) 1

12. What is the worst-case time complexity of an AVL tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)
Advance recursion problems and Optimization

1. What is the technique used to reduce the time complexity of a recursive function by storing the
results of subproblems in a cache?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: a) Dynamic Programming

2. What is the technique used to reduce the time complexity of a recursive function by breaking down
the problem into smaller subproblems?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: c) Divide and conquer

3. What is the technique used to reduce the time complexity of a recursive function by making a
choice and then backtracking if the choice leads to an incorrect solution?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: b) Backtracking
4. What is the technique used to reduce the time complexity of a recursive function by always making
the locally optimal choice?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: d) Greedy algorithm

5. How can the time complexity of a recursive function be reduced by eliminating redundant recursive
calls?

a) Memoization

b) Top-down approach

c) Bottom-up approach

d) Iterative approach

Answer: a) Memoization

6. How can the time complexity of a recursive function be reduced by eliminating overlapping
subproblems?

a) Memoization

b) Top-down approach

c) Bottom-up approach

d) Iterative approach

Answer: a) Memoization

7. What is the technique used to solve a problem by breaking it down into smaller subproblems and
solving each subproblem independently?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: c) Divide and conquer


8. What is the technique used to solve a problem by combining solutions to subproblems in a bottom-
up manner?

a) Dynamic programming

b) Backtracking

c) Divide and conquer

d) Greedy algorithm

Answer: a) Dynamic Programming

9. What is the technique used to solve a problem by using a combination of recursion and iteration?

a) Tail recursion optimization

b) Iterative deepening

c) Branch and bound

d) Recursive descent

Answer: b) Iterative optimization

10. What is the technique used to solve a problem by using a combination of recursion and
memorization?

a) Tail recursion optimization

b) Iterative deepening

c) Branch and bound

d) Recursive descent

Answer: a) Tail recursion optimization

11. What is the technique used to solve a problem by using a combination of recursion and dynamic
programming?

a) Tail recursion optimization

b) Iterative deepening

c) Branch and bound

d) Recursive descent

Answer: a) Tail recursion optimization


12. What is the technique used to solve a problem by using a combination of recursion and
backtracking?

a) Tail recursion optimization

b) Iterative deepening

c) Branch and bound

d) Recursive descent

Answer: d) Recursive descent


ST -IV
Red Black Trees: Balanced Trees - Red Black Trees, Graphs:
Graphs, Edge List, Adjacency Matrix, adjacency List, Depth first
traversal, Breadth first traversal, Shortest Path algorithms.

1. What is the name of the algorithm that is used to maintain balance in red-black trees?

a) AVL rotation

b) Red-black rotation

c) B-tree rotation

d) Hash table

Answer: b) Red-black rotation

2. What is the maximum height of a red-black tree with n nodes?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

3. What are the two possible colors of a node in a red-black tree?

a) Red and Black

b) Green and Black

c) Blue and Black

d) Yellow and Black

Answer: a) Red and Black


4. What is the property that must be maintained in a red-black tree to ensure that the tree remains
balanced?

a) Sorted order

b) Complete tree

c) Height-balanced

d) Color-balanced

Answer: d) Color-balanced

5. What is the time complexity of searching for a node in a red-black tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: O(log n)

6. How is the depth-first traversal implemented in a graph?

a) Using a queue

b) Using a stack

c) Using a tree

d) Using a hash table

Answer: b) Using a stack

7. How is the breadth-first traversal implemented in a graph?

a) Using a queue

b) Using a stack

c) Using a tree

d) Using a hash table

Answer: a) Using a queue


8. What are the different ways to represent a graph in memory?

a) Edge list, adjacency matrix, adjacency list

b) Hash table, tree, graph

c) Stack, queue, array

d) Linked list, array, matrix

Answer: a) Edge list, adjacency matrix, adjacency list

9. What is the difference between a red-black tree and a binary search tree?

a) Red-black tree is balanced, binary search tree is not.

b) Binary search tree has a search property, red-black tree does not.

c) Red-black tree has a color property, binary search tree does not.

d) Binary search tree is simpler, red-black tree is more complex.

Answer: a) Red-black tree is balanced, binary search tree is not

10.How is the balance of a red-black tree maintained?

a) By enforcing a fixed height for the tree

b) By enforcing a fixed number of nodes in the tree

c) By enforcing a balance property between the number of red and black nodes in the tree

d) By enforcing a balance property between the number of left and right nodes in the tree

Answer: c) By enforcing a balance property between the number of red and black nodes in the tree

11. What is the time complexity of inserting a node in a red-black tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)
12. What is the time complexity of deleting a node in a red-black tree?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: b) O(log n)

13. What is the technique used to find the shortest path between two nodes in a graph?

a) Dijkstra's algorithm

b) Bellman-Ford algorithm

c) Breadth-first search

d) Depth-first search

Answer: a) Dijkstra’s algorithm

14. What is the time complexity of Dijkstra's algorithm for finding the shortest path in a graph?

a) O(1)

b) O(log n)

c) O(n)

d) O(n log n)

Answer: d) O(n log n)


Tries: Implementing tries, Suffix Arrays: Suffix Arrays, Longest
Repeated string - Overlapping and Non overlapping

1. What is a tries data structure?

A) Hash table

B) Binary tree

C) Prefix tree

D) Heap

Answer: c) Prefix tree

2. How is a tries implemented in Java?

A) Using a linked list

B) Using a stack

C) Using a nested class structure with a character, a boolean flag and a map of child nodes

D) Using a queue

Answer: Using the nested class structure with a character, a boolean flag and a map of child nodes

3. What is a suffix array?

A) A data structure used to store all the possible prefixes of a given string in lexicographically sorted
order

B) A data structure used to store all the possible suffixes of a given string in lexicographically sorted
order

C) A data structure used to store all the possible substrings of a given string in lexicographically sorted
order

D) A data structure used to store all the unique characters of a given string in lexicographically sorted
order

Answer: b) A data structure used to store all the possible suffixes of a given string in lexicographically
sorted order
4. How is a suffix array implemented in Java?

A) Using a linked list

B) Using a stack

C) Using an array of integers sorted lexicographically based on the corresponding suffixes

D) Using a queue

Answer: c) Using a array of integers sorted lexicographically based on the corresponding suffixes

5. What is the longest repeated substring problem?

A) The task of finding the longest substring that appears in the input string only once

B) The task of finding the shortest substring that appears in the input string more than once

C) The task of finding the longest substring that appears in the input string more than once with
overlapping occurrences

D) The task of finding the longest substring that appears in the input string more than once with non-
overlapping occurrences

Answer: d) The task of finding the longest substring that appears in the input string more than once
with non-overlapping occurrences

6. How can we find the longest repeated substring with non-overlapping occurrences using suffix
arrays?

A) By comparing each pair of adjacent suffixes in the sorted array and find the longest common suffix
between them

B) By comparing each pair of adjacent suffixes in the sorted array and find the shortest common suffix
between them

C) By comparing each pair of adjacent suffixes in the sorted array and find the longest common prefix
between them

D) By comparing each pair of adjacent suffixes in the sorted array and find the shortest common
prefix between them

Answer: c) By comparing each pair of adjacent suffixes in the sorted array and find the longest
common prefix between them
7. What is the main advantage of using a tried data structure over a hash table for storing a collection
of strings

A) Tries use less memory

B) Tries have faster search time

C) Tries support prefix matching

D) Tries are more flexible in terms of data storage

Answer: c) Tries support prefix matching

8. What is the time complexity of inserting a new word into a trie?

A) O(n)

B) O(1)

C) O(log n)

D) O(n log n)

Answer: a) O(n)

9. How can suffix arrays be used to solve the longest common substring problem?

A) By comparing each pair of adjacent suffixes in the sorted array and find the longest common suffix
between them

B) By comparing each pair of adjacent suffixes in the sorted array and find the shortest common suffix
between them

C) By comparing each pair of adjacent suffixes in the sorted array and find the longest common prefix
between them

D) By comparing each pair of adjacent suffixes in the sorted array and find the shortest common
prefix between them

Answer: c) By comparing each pair of adjacent suffixes in the sorted array and find the longest
common prefix between them
10. What is the time complexity of constructing a suffix array for a given string?

A) O(n)

B) O(1)

C) O(log n)

D) O(n log n)

Answer: d) O(n log n)

11. What is the time complexity of finding the longest repeated substring with non-overlapping
occurrences using suffix arrays?

A) O(n^2)

B) O(n)

C) O(log n)

D) O(n log n)

Answer: b) O(n)

12. How can we find the longest repeated substring with overlapping occurrences using a tried?

A) By traversing the tried and counting the number of times each word appears

B) By traversing the tried and checking if any word appears more than once

C) By traversing the tried and checking if any word has more than one child

D) By traversing the tried and comparing the length of each word to find the longest one.

Answer: b) By traversing the tried and checking if any word appears more than once

You might also like