Data Structures & AlgorithmsHardTCS NQT · Accenture
Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the count of reachable paths.
Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the count of reachable paths.
def paths(g):
n, m = len(g), len(g[0])
dp = [[0] * m for _ in range(n)]
dp[0][0] = 1
for i in range(n):
for j in range(m):
if g[i][j] == '#':
dp[i][j] = 0
continue
if i:
dp[i][j] += dp[i - 1][j]
if j:
dp[i][j] += dp[i][j - 1]
return dp[-1][-1]
print(paths(['...', '.#.', '...']))What is printed?- A. 2
- B. 4
- C. 6
- D. 3
Correct answer: A. 2
There are C(4,2) = 6 monotone paths, and 4 of them pass through the blocked centre, leaving 2. The DP sums paths from above and left while skipping blocked cells.
More Coding Interview Problems questions
- Problem: What does the following print? python def add(x, bucket=[]): bucket.append(x) return bucket add(1) add(2) print(add(3))
- Problem: What does the following print for the array [7, 10, 4, 3, 20, 15] with k = 3? python import heapq def kth(a, k): h = [] for x in…
- Problem: What does the following print? python ops = [('b', 2), ('a', 2), ('c', 1), ('d', 3)] print([x for x, in sorted(ops, key=lambda t…
- Problem: Coins of values {1, 5, 6, 9} are available, unlimited copies each. What is the minimum number of coins needed to make exactly 11?
- Problem: In the O(n log n) longest increasing subsequence algorithm, the array tails[k] is maintained over the input. What does tails[k]…
- Problem: A dynamic array doubles its capacity whenever it is full and copies all elements. Which statement about n pushes is correct?