Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Find the maximum number of points you can collect in a grid going right/down from top-left to bottom-right and then reconstruct which direction the last move was.
Problem: Find the maximum number of points you can collect in a grid going right/down from top-left to bottom-right and then reconstruct which direction the last move was.
g = [[1, 3, 1, 5], [2, 2, 4, 1], [5, 0, 2, 3], [0, 6, 1, 2]]
R, C = 4, 4
dp = [[0] * C for _ in range(R)]
for r in range(R):
for c in range(C):
best = max(dp[r-1][c] if r else 0, dp[r][c-1] if c else 0) if (r or c) else 0
dp[r][c] = g[r][c] + best
last = 'down' if dp[R-2][C-1] >= dp[R-1][C-2] else 'right'
print(dp[R-1][C-1], last)What is printed?- A. 17 right
- B. 18 down
- C. 15 down
- D. 17 down
Correct answer: D. 17 down
dp[r][c] = g[r][c] + max(from above, from the left). The last column's dp values are 10, 11, 15, 17 and the last row's 8, 14, 15, 17, so the bottom-right cell is 2 + max(15, 15) = 17. The two predecessors tie at 15, and the >= in the reconstruction picks 'down' (coming from above). Path reconstruction must define tie-breaking explicitly. O(R·C).
More Coding Interview Problems questions
- Problem: n ≤ 10⁵ tasks with durations and deadlines; complete as many tasks as possible, one at a time, each finishing by its deadline…
- Problem: Find the median of two sorted arrays of sizes m and n (up to 10⁶ each) in better than linear time. What is the optimal complexity?
- Problem: A social graph has V = 10⁶ users and E = 10⁷ friendships. You must store it to run BFS. Which representation is appropriate and…
- Problem: Given 'a/b = k' equations (e.g. a/b = 2, b/c = 3), answer queries like a/c. Which model and algorithm fit?
- Problem: Evaluate division queries on the ratio graph a/b = 2, b/c = 3, d/e = 4. python from collections import defaultdict g =…
- Problem: Reverse a word after prepending a marker using Java's StringBuilder. java StringBuilder sb = new StringBuilder("interview")…