Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Row n of Pascal's triangle built in place, plus two checks.
Problem: Row n of Pascal's triangle built in place, plus two checks.
def row(n):
r = [1]
for _ in range(n): r = [1] + [r[i] + r[i+1] for i in range(len(r) - 1)] + [1]
return r
r = row(10)
print(r[5], sum(r), sum(r[::2]) - sum(r[1::2]))What is printed?- A. 210 1024 0
- B. 252 1023 1
- C. 252 2048 0
- D. 252 1024 0
Correct answer: D. 252 1024 0
Row 10 holds C(10, k); the middle entry is C(10, 5) = 252. The row sums to 2¹⁰ = 1024, and the alternating sum Σ(−1)ᵏC(10, k) = (1 − 1)¹⁰ = 0.
More Coding Interview Problems questions
- Problem: Find the maximum sum of a sub-rectangle whose sum does not exceed k (fix two columns, then 'max subarray sum ≤ k' with a sorted…
- Problem: What does the following print? It is the multiplicative inverse of 7 modulo 26. python print(pow(7, -1, 26))
- Problem: How many trailing zeros does 100! have? Run the code that counts factors of 5. python n = 100 c = 0 p = 5 while p <= n: c += n //…
- Problem: Starting from 27, the Collatz map is n -> n/2 if n is even and n -> 3n+1 if n is odd. How many steps does it take to first reach…
- Problem: Find the smallest positive integer n such that n! has at least 6 trailing zeros. Run the code. python def zeros(n): c = 0 p = 5…
- Problem: Find the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18] with the O(n log n) tails method…