Data Structures & AlgorithmsHardProduct Company SDE · graph round
Problem: A directed graph with V vertices is relaxed with Bellman-Ford. After V - 1 full rounds of relaxing all edges, one more round still lowers some distance. What does this prove?
- A. The shortest path to that vertex has one more edge than allowed
- B. The graph is disconnected from the source
- C. A negative cycle is reachable from the source
- D. All edge weights must be positive
Correct answer: C. A negative cycle is reachable from the source
Any simple shortest path has at most V - 1 edges, so a further improvement can only happen if the walk loops through a negative cycle reachable from the source.
More Coding Interview Problems questions
- Problem: A Fenwick tree of size 8 receives point adds (1,+5), (3,+2), (5,+7), (8,+1) with the usual add and prefix-query code, then prints…
- Problem: You want the maximum number of non-overlapping intervals. Which greedy rule is provably optimal?
- Problem: Count subarrays whose XOR is zero, for a = [1, 2, 3, 0, 3, 3]. python from collections import Counter def cnt(a): seen =…
- Problem: What does the following print? python print([format(i ^ (i >> 1), '03b') for i in range(4)])
- Problem: Count the pairs (i < j) with a[i] + a[j] < 6 for the array [5, 1, 4, 2, 3]. python def count(a, T): a = sorted(a) i, j = 0…
- Problem: Mo's algorithm answers q range queries on an array of length n by sorting queries into blocks of size about sqrt(n) and moving…