In Dijkstra's algorithm, how does it select the next node to visit?

  • It always selects the first node in the graph
  • It chooses nodes randomly
  • It picks the node with the largest tentative distance value
  • It selects the node with the smallest tentative distance value
Dijkstra's algorithm selects the next node to visit based on the smallest tentative distance value. It maintains a priority queue or a min-heap to efficiently retrieve the node with the minimum distance.

What is the main advantage of using DFS over BFS in certain scenarios?

  • Guaranteed shortest path
  • Higher speed in most cases
  • Lower memory consumption
  • Simplicity of implementation
The main advantage of using DFS over BFS in certain scenarios is the simplicity of implementation. DFS is often easier to implement and requires less memory overhead compared to BFS.

Under what circumstances would you prefer to use Prim's algorithm over Kruskal's, and vice versa?

  • Both algorithms are equivalent and can be used interchangeably.
  • Kruskal's is preferred for dense graphs, while Prim's is suitable for sparse graphs.
  • Prim's is always faster than Kruskal's regardless of the graph characteristics.
  • Prim's is preferred for dense graphs, while Kruskal's is suitable for sparse graphs.
Prim's algorithm is generally preferred for dense graphs, where the number of edges is close to the maximum possible edges. On the other hand, Kruskal's algorithm tends to perform better on sparse graphs, where the number of edges is much less than the maximum possible. The choice depends on the specific characteristics of the graph.

LCS can be applied to non-string data types such as _______ to find common elements in sequences.

  • Arrays, Linked lists
  • Numbers, Matrices
  • Stacks, Queues
  • Trees, Graphs
Longest Common Subsequence (LCS) is a versatile algorithm that can be applied to non-string data types such as trees and graphs. It is used to identify common elements in sequences, providing a valuable tool in various domains beyond traditional string processing.

A dynamic programming approach to finding the Longest Palindromic Substring typically involves constructing a _______ to store intermediate results.

  • Binary tree
  • Hash table
  • Memoization table
  • Priority queue
A dynamic programming approach to finding the Longest Palindromic Substring typically involves constructing a memoization table to store intermediate results. This table is used to avoid redundant computations by caching and reusing previously computed results during the recursive process.

What is the difference between Dijkstra's algorithm and breadth-first search (BFS)?

  • Dijkstra's is for finding connected components, BFS is for finding shortest paths
  • Dijkstra's is for weighted graphs, BFS is for unweighted graphs
  • Dijkstra's is only for directed graphs, BFS is for undirected graphs
  • Dijkstra's uses a stack, BFS uses a queue
The main difference lies in their applications - Dijkstra's algorithm is designed for finding the shortest path in weighted graphs, while BFS is used for exploring and finding the shortest paths in unweighted graphs.

Selection sort's time complexity remains _______ regardless of the input sequence.

  • O(log n)
  • O(n log n)
  • O(n)
  • O(n^2)
The time complexity of selection sort is O(n^2), and it remains the same regardless of the input sequence. This is because it involves nested loops to iterate over the elements for comparisons and swaps, resulting in quadratic time complexity.

The time complexity of binary search is _______ due to its divide-and-conquer approach.

  • O(1)
  • O(log n)
  • O(n)
  • O(n^2)
The time complexity of binary search is O(log n) due to its divide-and-conquer approach. This is because with each comparison, the search space is effectively halved.

Insertion Sort is particularly effective when the input array is nearly _______ sorted.

  • Completely
  • Partially
  • Randomly
  • Sequentially
Insertion Sort is particularly effective when the input array is nearly partially sorted. In such cases, the number of comparisons and swaps required is significantly reduced, making it efficient.

Suppose you are tasked with sorting a small array of integers, where most elements are already sorted in ascending order. Which sorting algorithm would be most suitable for this scenario and why?

  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • Selection Sort
Insertion Sort would be the most suitable algorithm for this scenario. It has an average-case time complexity of O(n), making it efficient for small arrays, especially when elements are mostly sorted. Its linear time complexity in nearly sorted arrays outperforms other algorithms.