Data Structures & AlgorithmsModerateAccenture · Cognizant coding round pattern
Problem: Count paths modulo 10⁹ + 7 from the top-left to the bottom-right of a 3 × 3 board whose centre is blocked, moving right, down or diagonally down-right.
Problem: Count paths modulo 10⁹ + 7 from the top-left to the bottom-right of a 3 × 3 board whose centre is blocked, moving right, down or diagonally down-right.
M = 10**9 + 7
g = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
dp = [[0] * 3 for _ in range(3)]; dp[0][0] = 1
for r in range(3):
for c in range(3):
if g[r][c] or (r, c) == (0, 0): continue
dp[r][c] = ((dp[r-1][c] if r else 0) + (dp[r][c-1] if c else 0) + (dp[r-1][c-1] if r and c else 0)) % M
print(dp[2][2], dp[1][2])What is printed?- A. 6 2
- B. 4 2
- C. 13 3
- D. 2 1
Correct answer: B. 4 2
dp[r][c] = paths from the top, the left and the top-left diagonal; a blocked cell stays 0. Row 0 and column 0 have 1 path each; (1, 2) gets 1 (from above) + 0 (blocked centre) + 1 (diagonal from (0, 1)) = 2, and symmetrically (2, 1) = 2; the corner gets 2 + 2 + 0 = 4. Without the block the count would be 13 (a Delannoy number). O(R·C).
More Coding Interview Problems questions
- 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…
- 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 =…