Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Dijkstra is run on a directed graph with arcs 0→1 (5), 0→2 (2), 2→1 (1), 1→3 (2), 2→3 (7), 3→4 (1), 2→4 (9). Each vertex is settled once, in order of distance. In what order are vertices 0–4 settled? Report the vertex that is settled third.
- A. 1
- B. 2
- C. 0
- D. 3
Correct answer: A. 1
Dijkstra settles vertices in increasing distance; here vertex 2 is reached cheaply and then vertex 1 improves through it. Settling vertices in the order they are first discovered gives a different third vertex.
More Coding Interview Problems questions
- Problem: A directed graph on vertices 0–4 has arcs 0→1, 1→2, 2→0, 2→3, 3→4. How many distinct simple directed cycles does it contain?
- Problem: An undirected graph on 6 vertices has edges 0-1, 1-2, 2-3, 3-0, 3-4, 4-5. Find the number of vertices whose removal leaves a…
- Problem: The rooted tree (root 0) has children 0: [1, 2], 1: [3, 4], 2: [5], 4: [6, 7], 5: [8]. Nodes are numbered in DFS pre-order with…
- Problem: A binary tree is given by a parent array where node i has parent p[i] (p[0] = −1 is the root): p = [−1, 0, 0, 1, 1, 2, 2, 5]. How…
- Problem: What is the minimum number of swaps of two neighbouring characters needed to turn "abcdef" into "fedcba"?
- Problem: Find the number of distinct strings obtained by reading the grid of letters ab cd moving right or down from the top-left, where…