Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP.
Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP.
n = 5
g = [[0] * n for _ in range(n)]
g[0][0] = 1
for i in range(n):
for j in range(n):
if (i, j) == (0, 0):
continue
if (i, j) == (2, 2):
g[i][j] = 0
continue
g[i][j] = (g[i - 1][j] if i else 0) + (g[i][j - 1] if j else 0)
print(g[4][4])What is printed?- A. 70
- B. 36
- C. 64
- D. 34
Correct answer: D. 34
C(8,4) = 70 paths minus C(4,2) · C(4,2) = 36 through (2,2) leaves 34. Forgetting the block gives 70, 36 counts the bad paths, and subtracting only 6 gives 64.
More Coding Interview Problems questions
- Problem: How many ordered pairs (x, y) with 1 <= x < y <= 50 have x y a perfect square? Run the code. python print(sum(1 for x in range(1…
- Problem: How many ways are there to write 50 as an ordered sum a^2 + b^2 with a, b >= 0? Run the code. python print(sum(1 for a in…
- Problem: What is the maximum product of 4 consecutive elements of [1, 2, 3, 4, 5, 6]? Run the code. python a = [1, 2, 3, 4, 5, 6]…
- Problem: How many subsequences of 'abcabc' equal the string 'abc'? Run the DP. python s, t = 'abcabc', 'abc' dp = [1] + [0] len(t) for ch…
- Problem: How many strings of length 5 over {a, b, c} have no two adjacent equal characters? Run the code. python from itertools import…
- Problem: How many inversions does [9, 7, 5, 3, 1, 2, 4, 6, 8] have? Run the code. python a = [9, 7, 5, 3, 1, 2, 4, 6, 8] print(sum(1 for i…