Data Structures & AlgorithmsHardTCS NQT Digital · Infosys
Problem: A road map has junctions 0–6 and two-way roads with lengths: 0-1 (4), 0-2 (1), 2-1 (2), 1-3 (5), 2-3 (8), 3-4 (3), 2-5 (10), 4-6 (2), 5-6 (1). What is the length of the shortest route from junction 0 to junction 6?
- A. 12
- B. 13
- C. 14
- D. 3
Correct answer: A. 12
Dijkstra: dist(2) = 1, dist(1) = 3, dist(3) = 8, dist(5) = 11, dist(4) = 11, and dist(6) = min(11 + 1, 11 + 2) = 12 via 0-2-5-6. The route through 3 and 4 gives 13, 0-1-3-4-6 gives 14, and 3 counts roads instead of length.
More Coding Interview Problems questions
- Problem: Starting with 8 singleton sets {0}..{7}, perform these unions in order: (0,1), (2,3), (1,3), (4,5), (6,7), (5,7), (3,4). After…
- Problem: A grid has '.' for open cells and '#' for walls: rows are ".#..#", ".#.##", "...#.", "##..#" and "#...." (5×5). Cells connect…
- Problem: How many simple cycles (cycles that visit no vertex twice, with 3 or more vertices) does the complete undirected graph K4 on…
- Problem: A rooted tree (root 1) has children lists: 1: [2, 3], 2: [4, 5, 6], 3: [7], 4: [], 5: [8], 6: [], 7: [9], 8: [], 9: []. How many…
- Problem: For the same rooted tree as the previous question (root 1: children 2, 3; 2: children 4, 5, 6; 3: child 7; 5: child 8; 7: child…
- Problem: In a binary tree with 7 nodes in level order [10, 5, 15, 2, 7, 12, 20] (missing children are shown as absent, so the array…