Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the endpoints? Run the DP.
Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the endpoints? Run the DP.
n = 4
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n + 1):
for j in range(n + 1):
if (i, j) == (0, 0):
continue
if i == j and i > 0 and i < n:
dp[i][j] = 0
continue
dp[i][j] = (dp[i - 1][j] if i else 0) + (dp[i][j - 1] if j else 0)
print(dp[n][n])What is printed?- A. 70
- B. 14
- C. 5
- D. 10
Correct answer: D. 10
Paths that avoid the interior diagonal stay strictly below or strictly above it, each counted by Catalan C₃ = 5, total 10. No restriction gives 70, 14 is C₄ (touching allowed), and 5 counts one side only.
More Coding Interview Problems questions
- Problem: What is the number of integers in [1, 10000] that are perfect squares and also end in the digit 9? Run the code. python…
- Problem: How many ways are there to split the string '1234' into two non-empty parts whose integer values are both divisible by 3? Run the…
- Problem: How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum that is a multiple of 4? Run the code. python from itertools…
- Problem: What is the minimum number of operations to make the array [5, 1, 4] strictly increasing where one operation increments any…
- Problem: What is the number of integers in [1, 999] whose digit product is 0? Run the code. python print(sum(1 for x in range(1, 1000) if…
- Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(ai, aj) = 1? Run the code. python from math import gcd a = [6, 10…