Data Structures & AlgorithmsHardTCS NQT · Accenture
Problem: In a binary-tree-free DFS on the undirected graph with edges 0-1, 0-2, 1-3, 2-4, 3-4, 4-5, starting from vertex 0 and always visiting the smallest unvisited neighbour first, what is the last vertex visited?
- A. 6
- B. 4
- C. 7
- D. 5
Correct answer: D. 5
Iterative DFS with a stack visits neighbours in the order they are pushed; pushing in reverse sorted order makes the smallest neighbour come off first. The last vertex is the leaf reached at the end of the deepest branch.
More Coding Interview Problems questions
- Problem: What does the following print? python print(sorted(['b', 'B', 'a', 'A']))
- Problem: Selection sort is run on [4, 3, 2, 1] in the usual way (for each position, find the minimum of the unsorted suffix and swap it…
- Problem: A Rule 90 cellular automaton on an infinite line starts with a single live cell. A cell's next state is 1 exactly when its two…
- Problem: A lucky 4-digit ticket has the sum of its first two digits equal to the sum of its last two digits. Each ticket number runs from…
- Problem: A grid of 5×5 cells has 'X' blocked cells at (0,2), (1,2), (2,2), (3,2) (row, column, 0-indexed); the rest are open. Cells…
- Problem: On a grid, a diagonal step costs 2 and a straight step costs 1 (moves are up, down, left, right, and the four diagonals). What is…