Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many monotone right/down paths go from (0,0) to (3,3) on a 4 x 4 grid when cell (1,2) is blocked? Run the DP.
Problem: How many monotone right/down paths go from (0,0) to (3,3) on a 4 x 4 grid when cell (1,2) is blocked? Run the DP.
g = [[0] * 4 for _ in range(4)]
g[0][0] = 1
for i in range(4):
for j in range(4):
if (i, j) == (1, 2):
g[i][j] = 0
continue
if (i, j) == (0, 0):
continue
g[i][j] = (g[i - 1][j] if i else 0) + (g[i][j - 1] if j else 0)
print(g[3][3])What is printed?- A. 11
- B. 14
- C. 9
- D. 12
Correct answer: A. 11
There are 20 unobstructed paths; 3 reach (1,2) and 3 leave it, so 9 pass through the blocked cell, leaving 11.
More Coding Interview Problems questions
- Problem: How many twin prime pairs (p, p + 2), both prime, have p + 2 below 10000? Run the sieve. python N = 10000 s = [True] (N + 1) s[0]…
- Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code. python def prime(n): return n > 1 and all(n % d for d in…
- Problem: How many integer lattice points (x, y) lie on the circle x^2 + y^2 = 25? Run the code. python print(sum(1 for x in range(-5, 6)…
- Problem: How many integers in [1, 10000] have decimal digits summing to 12? Run the code. python print(sum(1 for x in range(1, 10001) if…
- Problem: What is the length of the longest palindromic subsequence of 'character'? Run the DP. python def lps(s): n = len(s) dp = [[0] n…
- Problem: What is the minimum number of characters to insert to make 'leetcode' a palindrome? Use length minus longest palindromic…