Data Structures & AlgorithmsHardTCS NQT · Infosys coding round
Problem: An undirected road network has 5 towns numbered 0 to 4 and these roads with lengths: 0–1 (7), 0–2 (2), 2–1 (3), 1–3 (1), 2–3 (8), 3–4 (2), 2–4 (12). A courier starts at town 0. What is the length of the shortest route from town 0 to town 4?
- A. 10
- B. 8
- C. 14
- D. 12
Correct answer: B. 8
Dijkstra from town 0 gives 0→2→1→3→4 = 2 + 3 + 1 + 2 = 8. Taking the direct 0–1 road first gives 7 + 1 + 2 = 10, the fewest-roads route 0→2→4 costs 14, and 0→2→3→4 costs 12.
More Coding Interview Problems questions
- Problem: Nine nodes 0 to 8 start with no edges. These union operations are applied in order: (0,1), (2,3), (1,2), (4,5), (6,7), (7,8)…
- Problem: A tree has 8 nodes 0–7 and edges: 0–1, 1–2, 1–3, 3–4, 4–5, 3–6, 6–7. What is the diameter of the tree, measured as the number of…
- Problem: A directed graph has vertices 0–6 and arcs: 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 5→6, 6→6. A vertex lies on a cycle if it can reach…
- Problem: Count how many of these four undirected graphs are bipartite (2-colourable so that every edge joins different colours): G1: edges…
- Problem: Find the length of the longest palindromic substring of the string "abacdfgdcabazyx".
- Problem: Using insert, delete and replace, each costing 1, what is the minimum number of operations to convert "dragon" into "lantern"?