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?
  1. A. 210 1024 0
  2. B. 252 1023 1
  3. C. 252 2048 0
  4. 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

Exams that ask this topic