Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: A graph's vertices 0–4 are connected by edges 0-1 (1), 1-2 (1), 2-3 (1), 3-4 (1), 0-4 (5), 1-3 (3). What is the largest shortest-path distance from vertex 0 to any other vertex?
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 4
Dijkstra from vertex 0 gives distances 1, 2, 3, 4 along the path 0-1-2-3-4 (the direct edge of weight 5 is never used). The largest distance is the one to vertex 4, which is 4.
More Coding Interview Problems questions
- Problem: In a directed graph on vertices 0–3, the arcs are 0→1, 1→2, 2→0, 2→3. The vertex 3 has no outgoing arcs. How many vertices can…
- Problem: A rooted tree has root 1 and children 1: [2, 3, 4], 3: [5], 4: [6, 7]. The number of nodes on the longest root-to-leaf path…
- Problem: The string "abcdcba" is checked for palindromic substrings with an odd length of at least 5. How many such substrings are there?
- Problem: What is the number of distinct strings of length 4 over the alphabet {a, b} containing at least one 'a' and at least one 'b'?
- Problem: The string "aabbccddee" is compressed by keeping each character once for every run of length at least two, followed by the count…
- Problem: Calculate the number of ordered pairs (x, y) of integers with 1 ≤ x, y ≤ 20 for which x² + y² is divisible by 5.