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

Advance Competetive coding

Uploaded by

aryanworks010
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)
24 views

Advance Competetive coding

Uploaded by

aryanworks010
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/ 31

Loop Detection

1. What is a loop in a linked list?


a) A segment of the list pointing to NULL
b) A segment of the list where a node points to a previous node
c) A segment of the list where a node points to any other node in the list
d) A segment of the list containing only one node

2. Which of the following algorithms can be used to detect a loop in a linked list?
a) Breadth-first search (BFS)
b) Depth-first search (DFS)
c) Both BFS and DFS
d) Linear search

3. What is the time complexity of Floyd’s Cycle Detection Algorithm for detecting a loop in a
linked list?
a) O(n)
b) O(log n)
c) O(n^2)
d) O(1)

4. Floyd’s Cycle Detection Algorithm is also known as:


a) Tortoise and Hare Algorithm
b) Rabbit and Turtle Algorithm
c) Rabbit and Hare Algorithm
d) Turtle and Rabbit Algorithm

5. In Floyd’s Cycle Detection Algorithm, what is the purpose of the slow and fast pointers?
a) Slow pointer iterates through the list twice as fast as the fast pointer
b) Slow pointer iterates through the list one node at a time, while fast pointer iterates two
nodes at a time
c) Fast pointer iterates through the list twice as fast as the slow pointer
d) Slow pointer iterates through the list two nodes at a time, while fast pointer iterates one
node at a time

6. What is the space complexity of Floyd’s Cycle Detection Algorithm?


a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
7. Which of the following statements is true about the approach of marking visited nodes to
detect a loop in a linked list?
a) It requires additional space proportional to the size of the linked list
b) It guarantees a faster detection of the loop compared to other methods
c) It has a time complexity of O(log n)
d) It is not applicable for loop detection in linked lists

8. In the context of loop detection, what is a hash table used for?


a) To store the address of each visited node
b) To store the values of each node in the linked list
c) To calculate the hash value of each node
d) To store the pointers of each node in the linked list

9. In the context of loop detection in a linked list, what is a naive approach?


a) Using two pointers, one moves at twice the speed of the other
b) Using a hash table to store visited nodes
c) Traversing the list and checking if any node is visited more than once
d) Using recursive depth-first search (DFS)

10. Which of the following scenarios is NOT a suitable application for loop detection in a linked
list?
a) Detecting a cycle in a process scheduling algorithm
b) Verifying if a linked list is a palindrome
c) Finding the middle element of a linked list
d) Detecting a deadlock in a resource allocation system

11. Which of the following is NOT a characteristic of a linked list containing a loop?
a) It has a NULL pointer at the end
b) It has multiple paths to traverse the list
c) It has a finite number of nodes
d) It contains at least one node pointing to a previous node

12. In Floyd’s Cycle Detection Algorithm, how does the algorithm determine the presence of a
loop?
a) By checking if the slow pointer meets the fast pointer at any point
b) By comparing the values of adjacent nodes in the list
c) By traversing the list multiple times
d) By maintaining a count of the number of nodes visited
Segregate Even Odd in LL

1. What is the objective of the algorithm for segregating even and odd numbers in a linked list?
a) To rearrange the elements so that even numbers appear before odd numbers
b) To sort the elements in ascending order
c) To remove all odd numbers from the linked list
d) To remove all even numbers from the linked list

2. Which of the following is NOT a step involved in segregating even and odd numbers in a
linked list?
a) Traversing the linked list
b) Checking if a number is even or odd
c) Removing nodes containing even numbers
d) Reversing the order of nodes in the list

3. What is the time complexity of the algorithm for segregating even and odd numbers in a
linked list?
a) O(n)
b) O(log n)
c) O(n^2)
d) O(1)

4. What is the significance of maintaining two separate lists (even and odd) in the algorithm?
a) To store even and odd numbers separately for easier manipulation
b) To ensure all even numbers are adjacent to each other in the list
c) To sort the numbers into even and odd categories
d) To calculate the sum of even and odd numbers separately

5. Which of the following statements is true about the approach of moving even numbers to the
front of the linked list?
a) It requires modifying the values of even numbers
b) It maintains the relative order of even and odd numbers
c) It has a time complexity of O(log n)
d) It is not applicable for segregating even and odd numbers

6. In the context of segregating even and odd numbers in a linked list, what is a naive
approach?
a) Traversing the list and checking if any number is divisible by 2
b) Sorting the list using bubble sort algorithm
c) Removing nodes containing odd numbers
d) Using recursive depth-first search (DFS)
7. Which of the following scenarios is a suitable application for segregating even and odd
numbers in a linked list?
a) Sorting a list of names alphabetically
b) Separating positive and negative numbers in a list
c) Finding the median of a list of numbers
d) Calculating the sum of all elements in a list

8. In the context of the algorithm for segregating even and odd numbers, what is the
significance of the order of appearance of even and odd numbers?
a) It has no significance
b) Even numbers must appear before odd numbers
c) Odd numbers must appear before even numbers
d) The order depends on the value of the numbers

9. In the context of segregating even and odd numbers in a linked list, what is a more efficient
approach than brute-force traversal?
a) Using two pointers to traverse the list
b) Sorting the list using quicksort algorithm
c) Using recursive depth-first search (DFS)
d) Removing nodes containing odd numbers

10. Which of the following is a necessary condition for a linked list to be segregated into even
and odd numbers?
a) It must have at least two nodes
b) It must have all nodes pointing to the same node
c) It must contain both even and odd numbers
d) It must have a cycle in its structure

11. Which of the following scenarios is NOT a suitable application for segregating even and odd
numbers in a linked list?
a) Filtering out negative numbers from a list
b) Dividing a list of integers into two groups based on parity
c) Reversing the order of elements in a list
d) Grouping numbers based on their remainder when divided by 3
Merge Sort for DLL

1. Which of the following is NOT a step in the Merge Sort algorithm?


a) Merge
b) Partition
c) Divide
d) Conquer

2. What is the time complexity of Merge Sort for a Doubly Linked List (DLL) with n elements?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)

3. Which data structure is typically used to implement Merge Sort for a DLL?
a) Array
b) Stack
c) Queue
d) Recursion

4. In Merge Sort, how are two sorted sublists merged into a single sorted list?
a) By repeatedly swapping adjacent elements until the list is sorted
b) By recursively dividing the list into smaller sublists
c) By iteratively comparing elements from both sublists and merging them in sorted order
d) By selecting a pivot and partitioning the list around it

5. Which of the following statements about Merge Sort is true?


a) It is an in-place sorting algorithm
b) It has a worst-case time complexity of O(n^2)
c) It is not suitable for linked lists
d) It is a stable sorting algorithm

6. Which step of the Merge Sort algorithm involves dividing the list into smaller sublists?
a) Merge
b) Partition
c) Divide
d) Conquer

7. What is the space complexity of Merge Sort for a Doubly Linked List (DLL) with n elements?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)
8. Which of the following scenarios is a suitable application for Merge Sort?
a) Sorting a small array with less than 10 elements
b) Sorting a large dataset where memory usage is a concern
c) Sorting a linked list with variable-length elements
d) Sorting a dataset that is already partially sorted

9. Which step of the Merge Sort algorithm involves conquering and combining the sorted
sublists?
a) Merge
b) Partition
c) Divide
d) Conquer

10. In Merge Sort, what is the purpose of recursively dividing the list into smaller sublists?
a) To increase the time complexity
b) To decrease the space complexity
c) To sort the elements in descending order
d) To reduce the problem size into simpler subproblems

11. Which of the following statements is true about Merge Sort's stability?
a) Merge Sort is inherently unstable
b) Merge Sort maintains the relative order of equal elements
c) Merge Sort randomly shuffles equal elements
d) Merge Sort only works for datasets with unique elements
Sort the Bitonic DLL

1. What is a Bitonic sequence?


a) A sequence of numbers that first increases and then decreases
b) A sequence of numbers that first decreases and then increases
c) A sequence of numbers that are all zeros
d) A sequence of numbers that are all ones

2. What is the time complexity of the "Sort the Bitonic DLL" algorithm?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)

3. Which of the following is NOT a step in the "Sort the Bitonic DLL" algorithm?
a) Convert the doubly linked list to a Bitonic sequence
b) Split the Bitonic sequence into two sub-sequences
c) Merge the two sub-sequences into a single sorted sequence
d) Traverse the doubly linked list and update pointers accordingly

4. What is the purpose of converting a doubly linked list to a Bitonic sequence in the algorithm?
a) To increase the space complexity
b) To simplify the sorting process
c) To remove any existing cycles in the list
d) To decrease the time complexity

5. Which step of the "Sort the Bitonic DLL" algorithm involves dividing the Bitonic sequence into
two sub-sequences?
a) Convert
b) Split
c) Merge
d) Traverse

6. Which of the following statements is true about the Bitonic sort algorithm?
a) It only works for singly linked lists
b) It has a worst-case time complexity of O(n^2)
c) It is a stable sorting algorithm
d) It can be used to sort both ascending and descending sequences
7. In the "Sort the Bitonic DLL" algorithm, what is the significance of merging the two
sub-sequences?
a) To increase the time complexity
b) To ensure that the Bitonic property is preserved
c) To remove any duplicate elements
d) To convert the Bitonic sequence back to a doubly linked list

8. Which of the following scenarios is a suitable application for the "Sort the Bitonic DLL"
algorithm?
a) Sorting a circular linked list
b) Sorting a doubly linked list with randomly arranged elements
c) Removing all even numbers from a doubly linked list
d) Creating a Bitonic sequence from a doubly linked list

9. What is the primary advantage of the Bitonic sort algorithm?


a) It has a lower time complexity compared to other sorting algorithms
b) It is easy to implement for any type of input data
c) It guarantees stability in sorting
d) It can handle large datasets efficiently

10. Which step of the "Sort the Bitonic DLL" algorithm involves recursively sorting the
sub-sequences?
a) Convert
b) Split
c) Merge
d) Traverse

11. What is the space complexity of the "Sort the Bitonic DLL" algorithm?
a) O(1)
b) O(n log n)
c) O(n)
d) O(log n)

12. Which of the following is a necessary condition for the "Sort the Bitonic DLL" algorithm to
work correctly?
a) The Bitonic sequence must be cyclic
b) The Bitonic sequence must have at least one peak
c) The Bitonic sequence must be sorted in ascending order
d) The Bitonic sequence must be sorted in descending order
Minimum Stack

1. What is the Minimum Stack Algorithm primarily used for?


a) Sorting elements in ascending order
b) Finding the minimum element in a stack in constant time
c) Removing duplicate elements from a stack
d) Reversing the order of elements in a stack

2. Which data structure is commonly used to implement the Minimum Stack Algorithm?
a) Array
b) Linked List
c) Queue
d) Binary Tree

3. In the Minimum Stack Algorithm, how is the minimum element stored?


a) It is stored separately in a variable
b) It is pushed onto the stack
c) It is maintained alongside each element in the stack
d) It is stored in a separate stack

4. Which operation of the Minimum Stack Algorithm ensures that the minimum element is
always up to date?
a) Push
b) Pop
c) Peek
d) Update

5. What happens to the minimum element in the stack when the current minimum element is
popped?
a) It is automatically updated to the next minimum element
b) It remains the same until explicitly changed
c) It becomes undefined
d) None of the above

6. Which operation of the Minimum Stack Algorithm has a constant time complexity?
a) Push
b) getMin
c) Pop
d) Both A and B
7. Which of the following statements best describes the advantage of the Minimum Stack
Algorithm?
a) It reduces the time complexity of common stack operations.
b) It eliminates the need for auxiliary data structures.
c) It guarantees constant-time access to the maximum element in the stack.
d) It ensures that the stack always remains sorted in ascending order.
The Celebrity Problem

1. Which optimization technique can be used to reduce the number of comparisons in the
Celebrity Problem algorithm?
a) Dynamic programming
b) Binary search
c) Memoization
d) Greedy algorithm

2. In the context of the Celebrity Problem algorithm, what is the significance of a person being a
"candidate"?
a) They are the most outgoing individual in the group
b) They are known by everyone but know no one.
c) They are potentially the celebrity but require validation.
d) They have the highest number of connections in the network.

3. How does the Divide and Conquer strategy improve the efficiency of the Celebrity Problem
algorithm?
a) It reduces the number of comparisons by half in each step.
b) It eliminates the need for comparisons altogether.
c) It guarantees finding the celebrity in constant time.
d) It ensures fairness among all individuals in the group.

4. Which scenario best illustrates the application of the Celebrity Problem algorithm in a
real-world setting?
a) Determining the most influential person in a social network.
b) Identifying the person with the highest salary in a company.
c) Selecting the most skilled athlete for a sports team.
d) Finding the fastest route in a transportation network.

5. What is the space complexity of the optimized Celebrity Problem algorithm?


a) O(n)
b) O(1)
c) O(n^2)
d) O(log n)

6. Which algorithmic approach is commonly used to solve the Celebrity Problem efficiently?
a) Depth-First Search (DFS)
b) Breadth-First Search (BFS)
c) Floyd-Warshall algorithm
d) QuickSelect algorithm
7. Which complexity class best describes the time complexity of the optimized Celebrity Problem
algorithm?
a) O(n)
b) O(log n)
c) O(n log n)
d) O(n^2)
Iterative Tower of Hanoi

1. Which data structure is commonly used in the iterative Tower of Hanoi algorithm to simulate
the movement of disks?
a) Stack
b) Queue
c) Linked List
d) Binary Tree

2. What is the time complexity of the iterative Tower of Hanoi algorithm for solving the problem
with( n ) disks?
a) O(1)
b) O(log n)
c) O(n)
d) O(2^n)

3. In the iterative Tower of Hanoi algorithm, how are the disks represented?
a) As integers
b) As nodes in a graph
c) As elements in an array
d) As objects in a stack

4. Which iterative technique is primarily used in the Tower of Hanoi algorithm to move disks
between pegs?
a) Depth-First Search (DFS)
b) Breadth-First Search (BFS)
c) Dynamic programming
d) Greedy algorithm

5. What is the space complexity of the iterative Tower of Hanoi algorithm?


a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

6. In the iterative Tower of Hanoi algorithm, how many moves are required to solve the problem
with ( n ) disks?
a) ( n )
b) ( 2^n )
c) ( 2^n - 1 )
d) ( 3^n - 1 )
7. Which of the following statements is true about the iterative Tower of Hanoi algorithm?
a) It relies on recursive function calls.
b) It always moves disks in a clockwise direction.
c) It guarantees the shortest possible sequence of moves.
d) It requires an auxiliary stack for implementation.

8. Which iterative strategy is employed to ensure the correct sequence of moves in the Tower of
Hanoi algorithm?
a) Preorder traversal
b) Postorder traversal
c) Inorder traversal
d) Level order traversal

9. What is the primary advantage of the iterative Tower of Hanoi algorithm over the recursive
approach?
a) Reduced time complexity
b) Elimination of stack overflow issues
c) Simpler implementation
d) Guarantee of optimal solution

10. How does the iterative Tower of Hanoi algorithm ensure that the disks are moved according
to the rules of the puzzle?
a) By using a priority queue
b) By maintaining a stack of valid moves
c) By checking the legality of each move
d) By randomly selecting moves until the puzzle is solved
Stock Span Problem

1. Which data structure is commonly used to efficiently solve the Stock Span Problem?
a) Stack
b) Queue
c) Heap
d) Linked List

2. What is the time complexity of the most efficient algorithm for solving the Stock Span
Problem?
a) O(n)
b) O(log n)
c) O(n^2)
d) O(2^n)

3. In the context of the Stock Span Problem, what does the "span" of a stock on a given day
represent?
a) The highest price of the stock on that day.
b) The difference between the highest and lowest prices of the stock on that day.
c) The total number of shares of the stock sold on that day.
d) The number of consecutive days before the current day where the stock price was less
than or equal to the current day's price.

4. What is the primary advantage of using a stack-based approach to solve the Stock Span
Problem?
a) It guarantees optimal results in all cases.
b) It requires less memory compared to other approaches.
c) It ensures constant-time access to stock prices.
d) It reduces the time complexity of the algorithm.

5. Which of the following is a suitable application of the Stock Span Problem algorithm?
a) Analyzing the sentiment of social media posts related to a stock.
b) Forecasting the future price of a stock based on historical data.
c) Calculating the moving average of a stock's price over a period.
d) Identifying patterns in stock price movements to inform trading strategies.

6. What is the space complexity of the most efficient algorithm for solving the Stock Span
Problem?
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
7. Which of the following statements accurately describes the significance of the Stock Span
Problem algorithm?
a) It helps investors determine the intrinsic value of a stock.
b) It provides insights into the overall market sentiment towards a stock.
c) It assists in identifying periods of high volatility in a stock's price.
d) It aids in understanding the strength of a trend in a stock's price movement.
Stack Permutation

1. Which data structure is commonly used in the Stack Permutation Algorithm?


a) Array
b) Linked List
c) Stack
d) Queue

2. What is the time complexity of the Stack Permutation Algorithm?


a) O(n)
b) O(n log n)
c) O(n^2)
d) O(2^n)

3. In the context of the Stack Permutation Algorithm, what does a valid permutation imply?
a) The permutation can be achieved using only stack operations.
b) The permutation is sorted in ascending order.
c) The permutation contains distinct elements.
d) The permutation has the maximum depth possible.

4. Which operation is typically performed on the stack in the Stack Permutation Algorithm?
a) Push
b) Pop
c) Peek
d) Enqueue

5. Which of the following scenarios best illustrates the application of the Stack Permutation
Algorithm?
a) Generating all possible combinations of characters in a string.
b) Determining whether a given expression has balanced parentheses.
c) Sorting a list of integers using the quicksort algorithm.
d) Finding the maximum element in a stack.

6. Which of the following statements accurately describes the significance of the Stack
Permutation Algorithm?
a) It provides a method for generating permutations without using recursion.
b) It guarantees the generation of all possible permutations for a given sequence.
c) It ensures that a given sequence is sorted in ascending order.
d) It facilitates the conversion of infix expressions to postfix notation.
Priority Queue using DLL

1. What is the primary advantage of using a Doubly Linked List (DLL) to implement a Priority
Queue?
a) Constant time complexity for all operations
b) Efficient insertion and deletion of elements in any position
c) Automatic sorting of elements based on priority
d) Constant space complexity for all operations

2. Which operation is typically performed with the highest time complexity in a Priority Queue
implemented using a Doubly Linked List?
a) Insertion
b) Deletion
c) Peek
d) Sorting

3. In a Priority Queue implemented using a Doubly Linked List, how are elements organized
based on priority?
a) Elements with higher priority are placed at the beginning of the list
b) Elements with lower priority are placed at the end of the list
c) Elements are sorted based on priority during insertion
d) Elements are sorted based on priority during deletion

4. What is the time complexity of inserting an element into a Priority Queue implemented using
a Doubly Linked List?
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

5. How does the Priority Queue using a Doubly Linked List handle elements with equal priority?
a) They are inserted randomly into the list.
b) They are placed based on their order of insertion.
c) They are sorted based on a secondary criterion.
d) They are stored in a separate data structure.

6. Which operation retrieves the element with the highest priority from a Priority Queue
implemented using a Doubly Linked List?
a) Insert
b) Delete
c) Peek
d) Search
7. What is the primary drawback of using a Doubly Linked List to implement a Priority Queue?
a) Limited flexibility in operations
b) Inefficient memory usage
c) Complexity in maintaining sorted order
d) Difficulty in handling priority changes

8. Which scenario is most suitable for using a Priority Queue implemented with a Doubly Linked
List?
a) Real-time systems where constant time complexity is crucial
b) Large-scale data processing where memory usage is a concern
c) Applications requiring dynamic sorting of elements
d) Situations with frequent priority changes

9. What is the space complexity of a Priority Queue implemented using a Doubly Linked List?
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

10. Which operation in the Priority Queue using a Doubly Linked List typically has the highest
space complexity?
a) Insert
b) Delete
c) Peek
d) Search
Sort the Queue without extra space

1. What is the primary objective of sorting a queue without using extra space?
a) To minimize time complexity
b) To minimize space complexity
c) To maintain the original order of elements
d) To maximize the efficiency of the sorting algorithm

2. Which sorting algorithm is commonly used to sort a queue without using extra space?
a) Quick Sort
b) Merge Sort
c) Insertion Sort
d) Bubble Sort

3. What is the time complexity of the most efficient algorithm for sorting a queue without using
extra space?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(2^n)

4. How does the sorting algorithm handle the elements in the queue without using extra space?
a) By creating a copy of the queue for sorting
b) By using a separate data structure to store sorted elements
c) By rearranging elements within the original queue
d) By using recursion to sort individual elements

5. What is the primary challenge of sorting a queue without using extra space?
a) Minimizing time complexity
b) Handling priority changes of elements
c) Ensuring stability of sorting
d) Maintaining the FIFO (First-In-First-Out) order

6. Which operation is commonly performed during the sorting process to compare and
rearrange elements?
a) Swap
b) Reverse
c) Shuffle
d) Concatenate
7. What is the space complexity of the most efficient algorithm for sorting a queue without using
extra space?
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

8. Which of the following statements accurately describes the significance of sorting a queue
without using extra space?
a) It ensures stability in the sorting process.
b) It reduces the time complexity of the sorting algorithm.
c) It maintains the FIFO order while rearranging elements.
d) It provides a memory-efficient approach to sorting.
Max Sliding WIndow

1. What is the primary objective of the Max Sliding Window algorithm?


a) To find the maximum element in a given array.
b) To find the maximum element in each subarray of a given array.
c) To find the maximum element within a sliding window of a given size moving across a given
array.
d) To find the maximum sum of elements within a sliding window of a given size moving
across a given array.

2. Which data structure is commonly used to efficiently implement the Max Sliding Window
algorithm?
a) Array
b) Linked List
c) Stack
d) Deque (Double-ended queue)

3. What is the time complexity of the Max Sliding Window algorithm for an array of size n and a
sliding window size k?
a) O(n)
b) O(k)
c) O(n log k)
d) O(nk)

4. In the Max Sliding Window algorithm, how is the maximum element within each sliding
window efficiently determined?
a) By iterating through each window and finding the maximum element.
b) By maintaining a priority queue of elements within the window.
c) By using dynamic programming to precompute maximums.
d) By using a deque to store indices of elements in decreasing order of value.

5. What is the space complexity of the Max Sliding Window algorithm?


a) O(1)
b) O(k)
c) O(n)
d) O(nk)

6. What is the primary advantage of using a deque in the Max Sliding Window algorithm?
a) Constant time complexity for all operations
b) Efficient removal of elements from both ends
c) Automatic sorting of elements within the window
d) Minimization of memory usage
7. Which factor affects the time complexity of the Max Sliding Window algorithm the most?
a) Size of the array
b) Size of the sliding window
c) Number of distinct elements in the array
d) Distribution of elements within the array

8. Which of the following statements accurately describes the significance of the Max Sliding
Window algorithm?
a) It allows for efficient sorting of a given array.
b) It provides a memory-efficient approach to finding maximum elements in a moving window.
c) It ensures stable sorting of elements within a sliding window.
d) It guarantees optimal time complexity for all array sizes.
Recover the BST

1. Which algorithm is used to recover a binary search tree (BST) from two of its swapped
nodes?
A) Depth First Search (DFS)
B) Breadth First Search (BFS)
C) Inorder Traversal
D) Preorder Traversal
Correct Answer: C) Inorder Traversal

2. In the Recover the BST algorithm, what is the time complexity?


A) O(n)
B) O(log n)
C) O(n^2)
D) O(n log n)
Correct Answer: A) O(n)

3. Which of the following is NOT a step in the algorithm to recover a BST?


A) Perform an inorder traversal to find swapped nodes
B) Swap the values of the two nodes found
C) Reconstruct the entire tree from scratch
D) Correct the swapped nodes
Correct Answer: C) Reconstruct the entire tree from scratch

4. In Recover the BST, why is an inorder traversal used?


A) It guarantees that nodes are visited in sorted order
B) It guarantees that nodes are visited in reverse order
C) It guarantees that nodes are visited in random order
D) It guarantees that nodes are visited in breadth-first order
Correct Answer: A) It guarantees that nodes are visited in sorted order

5. What happens if more than two nodes are swapped in a BST?


A) The algorithm will fail
B) The algorithm will still work but might not recover the original tree
C) The algorithm will recover the original tree correctly
D) The algorithm will recover the original tree with fewer steps
Correct Answer: B) The algorithm will still work but might not recover the original tree

6. What is the worst-case space complexity of the Recover the BST algorithm?
A) O(n)
B) O(log n)
C) O(n^2)
D) O(1)
Correct Answer: D) O(1)
7. Which of the following is NOT a requirement for a tree to be considered a BST?
A) Each node has at most two children
B) Left subtree of a node contains only nodes with keys less than the node's key
C) Right subtree of a node contains only nodes with keys greater than the node's key
D) The height of the left and right subtrees differs by at most one
Correct Answer: D) The height of the left and right subtrees differs by at most one

8. In Recover the BST, how are swapped nodes identified during the inorder traversal?
A) By comparing each node with its parent
B) By comparing each node with its left child
C) By comparing each node with its right child
D) By comparing each node with its successor
Correct Answer: D) By comparing each node with its successor
Left view, Right view, Top view, Bottom view, Horizontal view, Vertical view

1. What does the "left view" of a binary tree represent?


A) Nodes seen from the left side when the tree is viewed from the root
B) Nodes seen from the right side when the tree is viewed from the root
C) Nodes seen from the left side when the tree is viewed from the bottom
D) Nodes seen from the right side when the tree is viewed from the bottom
Correct Answer: A) Nodes seen from the left side when the tree is viewed from the root

2. Which view of a binary tree is often used in applications where only a subset of the nodes
needs to be processed?
A) Left view
B) Right view
C) Top view
D) Bottom view
Correct Answer: C) Top view

3. In a binary tree, what does the "bottom view" represent?


A) Nodes seen from the bottom when the tree is viewed from the root
B) Nodes seen from the top when the tree is viewed from the root
C) Nodes seen from the bottom when the tree is viewed from the ground level
D) Nodes seen from the top when the tree is viewed from the ground level
Correct Answer: C) Nodes seen from the bottom when the tree is viewed from the ground
level

4. Which view of a binary tree shows the nodes that are visible from the root's right side?
A) Left view
B) Right view
C) Top view
D) Bottom view
Correct Answer: B) Right view

5. In a binary tree, what does the "horizontal view" represent?


A) Nodes seen from the left when the tree is viewed from a horizontal angle
B) Nodes seen from the right when the tree is viewed from a horizontal angle
C) Nodes seen from the top when the tree is viewed from a horizontal angle
D) Nodes seen from the bottom when the tree is viewed from a horizontal angle
Correct Answer: A) Nodes seen from the left when the tree is viewed from a horizontal angle
6. Which view of a binary tree is useful in scenarios where nodes at different heights need to be
distinguished?
A) Horizontal view
B) Vertical view
C) Top view
D) Bottom view
Correct Answer: B) Vertical view

7. What is the time complexity of finding the top view of a binary tree using a hash map?
A) O(n)
B) O(log n)
C) O(n log n)
D) O(n^2)
Correct Answer: A) O(n)

8. Which view of a binary tree shows the nodes that are visible from the root's left side?
A) Left view
B) Right view
C) Top view
D) Bottom view
Correct Answer: A) Left view

9. Which view of a binary tree shows the nodes that are visible from the root's top side?
A) Left view
B) Right view
C) Top view
D) Bottom view
Correct Answer: C) Top view

10. Which view of a binary tree is useful in scenarios where nodes at different depths need to be
distinguished?
A) Horizontal view
B) Vertical view
C) Top view
D) Bottom view
Correct Answer: D) Bottom view
Vertical Order Traversal:

1. What does the "vertical order traversal" of a binary tree mean?


A) Traversing nodes from top to bottom
B) Traversing nodes from left to right
C) Traversing nodes from top to bottom and from left to right within each vertical column
D) Traversing nodes from bottom to top
Correct Answer: C) Traversing nodes from top to bottom and from left to right within each
vertical column

2. Which data structure is commonly used to implement vertical order traversal?


A) Array
B) Linked List
C) Queue
D) Stack
Correct Answer: C) Queue

3. In vertical order traversal, how are nodes with the same horizontal distance from the root
processed?
A) They are processed in ascending order of their vertical distance
B) They are processed in descending order of their vertical distance
C) They are processed in the order they appear in the input
D) They are processed in random order
Correct Answer: C) They are processed in the order they appear in the input

4. What is the time complexity of vertical order traversal of a binary tree with 'n' nodes?
A) O(n)
B) O(log n)
C) O(n^2)
D) O(n log n)
Correct Answer: A) O(n)

5. Which traversal technique is commonly used for vertical order traversal?


A) Preorder Traversal
B) Inorder Traversal
C) Postorder Traversal
D) Level Order Traversal
Correct Answer: D) Level Order Traversal

6. In vertical order traversal, how are nodes in the same vertical column ordered?
A) In ascending order of their level in the tree
B) In descending order of their level in the tree
C) In the order they were inserted into the tree
D) In random order
Correct Answer: C) In the order they were inserted into the tree

7. What is the space complexity of vertical order traversal?


A) O(n)
B) O(log n)
C) O(n^2)
D) O(1)
Correct Answer: A) O(n)

8. In vertical order traversal, how are nodes at different horizontal distances from the root
processed?
A) They are processed in ascending order of their horizontal distance
B) They are processed in descending order of their horizontal distance
C) They are processed in the order they appear in the input
D) They are processed in random order
Correct Answer: A) They are processed in ascending order of their horizontal distance

9. Which operation is frequently used in implementing vertical order traversal?


A) Dequeue
B) Pop
C) Enqueue
D) Push
Correct Answer: C) Enqueue
Boundary Traversal

1. What does "boundary traversal" of a binary tree involve?


A) Traversing all the boundary nodes of the tree in a clockwise direction
B) Traversing all the boundary nodes of the tree in a counterclockwise direction
C) Traversing all the leaf nodes of the tree
D) Traversing all the internal nodes of the tree
Correct Answer: B) Traversing all the boundary nodes of the tree in a counterclockwise
direction

2. What is the time complexity of boundary traversal of a binary tree with 'n' nodes?
A) O(n)
B) O(log n)
C) O(n^2)
D) O(n log n)
Correct Answer: A) O(n)

4. In boundary traversal, how are the left boundary nodes of a binary tree processed?
A) In a top-down manner
B) In a bottom-up manner
C) In a clockwise manner
D) In a counterclockwise manner
Correct Answer: A) In a top-down manner

5. Which traversal technique is commonly used for boundary traversal?


A) Preorder Traversal
B) Inorder Traversal
C) Postorder Traversal
D) Level Order Traversal
Correct Answer: A) Preorder Traversal

6. In boundary traversal, how are the right boundary nodes of a binary tree processed?
A) In a top-down manner
B) In a bottom-up manner
C) In a clockwise manner
D) In a counterclockwise manner
Correct Answer: B) In a bottom-up manner

7. What is the space complexity of boundary traversal?


A) O(n)
B) O(log n)
C) O(n^2)
D) O(1)
Correct Answer: A) O(n)
8. In boundary traversal, how are the leaf nodes of a binary tree processed?
A) In a top-down manner
B) In a bottom-up manner
C) In a clockwise manner
D) In a counterclockwise manner
Correct Answer: D) In a counterclockwise manner

9. Which operation is frequently used in implementing boundary traversal?


A) Push
B) Pop
C) Enqueue
D) Dequeue
Correct Answer: A) Push

You might also like