Data Structures & AlgorithmsVery hardCodeforces Div2 B · DP warm-up
Problem: A graph may contain negative edge weights but no negative cycle. Which single-source shortest path algorithm handles this correctly, and what is its time complexity?
- A. Dijkstra's algorithm, O((V+E) log V)
- B. Bellman-Ford, O(VE)
- C. Floyd-Warshall, O(V^2)
- D. BFS, O(V+E)
Correct answer: B. Bellman-Ford, O(VE)
Bellman-Ford relaxes every edge V-1 times, correctly propagating negative weights (and can detect a negative cycle with one extra pass), at the cost of O(VE) instead of Dijkstra's faster but negative-weight-unsafe approach.
More Coding Interview Problems questions
- Problem: What is the time complexity of the Floyd-Warshall algorithm for finding shortest paths between every pair of nodes in a graph…
- Problem: Which of these best describes why Kruskal's MST algorithm needs a Union-Find (disjoint set) structure?
- Problem: Between Kruskal's and Prim's MST algorithms, which is typically preferred for a dense graph represented with an adjacency matrix?
- 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…