Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE
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 the minimum cost to go from (0,0) to (3,5)?
- A. 8
- B. 5
- C. 10
- D. 6
Correct answer: A. 8
A diagonal step (cost 2) is never cheaper than two straight steps, so the cost is the Manhattan distance 3 + 5 = 8 (equivalently 3 diagonals plus 2 straight steps). Pricing diagonals at 1 gives the Chebyshev distance 5, pricing every one of max(3, 5) steps at 2 gives 10, and counting only the 3 diagonal steps gives 6.
More Coding Interview Problems questions
- Problem: The cycle graph C8 has vertices 0–7 with i adjacent to (i+1) mod 8. How many unordered pairs of vertices are at distance exactly…
- Problem: A height-balanced binary search tree is built over the sorted keys 1, 2, …, 10 by always taking the middle element as the root…
- Problem: Given the parent array p = [−1, 0, 0, 1, 1, 2, 4, 4] for nodes 0–7 (node 0 is the root), how many nodes have exactly one child?
- Problem: A binary tree in level order (None for absent nodes) is [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]. How many root-to-leaf…
- Problem: Count the odd-length palindromic substrings of "racecarxyz" that are centred at index 3 (the 'e' of "racecar"), where the centre…
- Problem: What is the lexicographically next permutation of the string "bcdea" (the next arrangement in dictionary order using the letters…