Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many Dyck words (balanced bracket sequences) of length 10 exist? Run the DP.
Problem: How many Dyck words (balanced bracket sequences) of length 10 exist? Run the DP.
n = 5
C = [1] + [0] * n
for m in range(1, n + 1):
C[m] = sum(C[i] * C[m - 1 - i] for i in range(m))
print(C[n])What is printed?- A. 252
- B. 50
- C. 14
- D. 42
Correct answer: D. 42
Dyck words of length 10 are Catalan(5) = C(10,5)/6 = 42. 252 = C(10,5) ignores balance, dividing by n instead of n + 1 gives 50, and 14 is Catalan(4).
More Coding Interview Problems questions
- Problem: How many permutations of 1..5 have exactly 2 fixed points? Run the code. python from itertools import permutations print(sum(1…
- Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code. python n = 97 c = 0 while n != 1: n…
- Problem: How many subsets of {1, 2, ..., 12} have an odd sum? Run the code. python print(sum(1 for m in range(1 << 12) if sum(i + 1 for i…
- Problem: What is the largest value of x such that x^3 <= 10^12 (integer cube root)? Run the binary search. python lo, hi = 0, 10 6 while…
- Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code. python print(sum(1 for x in range(1, 1001) if…
- Problem: Which algorithm finds the convex hull of n points in O(n log n) by sorting and building lower and upper chains?