Data Structures & AlgorithmsModerateTCS NQT · graph theory
Problem: Why does BFS find shortest paths in an unweighted graph in O(V + E) time?
- A. It relaxes every edge repeatedly until no change
- B. It sorts edges by weight first
- C. It needs a priority queue with decrease-key
- D. It explores vertices in nondecreasing distance order, and each vertex and edge is processed once
Correct answer: D. It explores vertices in nondecreasing distance order, and each vertex and edge is processed once
Queue order keeps distances nondecreasing, so the first time a vertex is reached gives its shortest distance. Each vertex enters the queue once and each edge is scanned once.
More Coding Interview Problems questions
- Problem: Which data structure supports inserting values, deleting values and answering 'how many stored values are at most x' in O(log n)…
- Problem: Which statement about the Z-function of a string of length n is correct?
- Problem: How many subarrays of [2, 3, 4, 5] contain exactly one odd number? Run the code. python a = [2, 3, 4, 5] print(sum(1 for i in…
- Problem: What is the length of the longest subarray of [1, 2, 3, 4, 5] whose sum is at most 10? Run the two-pointer code. python a = [1…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] a[j] at most 10? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i…
- Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code. python a = [3, 1, 2] mins =…