Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Vertices 0–4 have undirected edges 0-1 (3), 1-2 (1), 2-3 (4), 3-4 (2), 0-4 (6), 1-4 (5). A path's cost is the sum of its edge weights. Among all simple paths from 0 to 3, what is the minimum cost?
- A. 8
- B. 10
- C. 16
- D. 21
Correct answer: A. 8
Both 0→1→2→3 (3 + 1 + 4) and 0→4→3 (6 + 2) cost 8, and no simple path is cheaper. 0→1→4→3 costs 10, the longest route 0→4→1→2→3 costs 16, and 21 sums every edge.
More Coding Interview Problems questions
- Problem: A directed graph has vertices 0–3 with arcs 0→1, 1→2, 2→1, 2→3. Using DFS from vertex 0 with children visited in increasing…
- Problem: Tree with root 1 and children 1: [2, 3], 2: [4, 5], 4: [6]. What is the number of nodes in the subtree of node 2 (node 2 counts…
- Problem: The string "abracadabra" contains the substring "abra" at several positions. How many occurrences of "abra" are there (overlaps…
- Problem: The string "abcdefghij" is split into blocks of length 4 from the left (the last block may be shorter). How many blocks have…
- Problem: The number of permutations of the digits 1, 2, 3, 4 in which the digit 1 is not in the first position and 4 is not in the last…
- Problem: Cars pass a toll booth at times 2, 5, 6, 10, 11, 15 (minutes). A car may pass only if at least 3 minutes have elapsed since the…