Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: Between Kruskal's and Prim's MST algorithms, which is typically preferred for a dense graph represented with an adjacency matrix?
- A. Prim's algorithm (O(V^2) without a heap, growing one tree from a start vertex)
- B. Both are always identical in performance regardless of density
- C. Neither works on dense graphs
- D. Kruskal's algorithm (O(E log E), sorting all edges first)
Correct answer: A. Prim's algorithm (O(V^2) without a heap, growing one tree from a start vertex)
On a dense graph E approaches V^2, making Kruskal's O(E log E) sorting step costly; Prim's array-based O(V^2) implementation (no heap needed) avoids sorting all edges and performs better for dense graphs.
More Coding Interview Problems questions
- Problem: Which statement correctly contrasts 0/1 knapsack and fractional knapsack?
- Problem: Activity selection (pick the maximum number of non-overlapping intervals from a set) is solved optimally by which greedy strategy?
- Problem: Which of these coin systems is a classic counterexample where the greedy 'always take the largest coin' algorithm fails to find…
- Problem: Huffman coding builds an optimal prefix-free binary code for a set of symbol frequencies. Which greedy step does it repeat?
- Problem: What is the time complexity of computing the longest increasing subsequence using patience-sorting with binary search, versus the…
- Problem: The longest common subsequence (LCS) of two strings of length n and m is typically solved with which approach and complexity?