Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 4 8 2 1; 4 3 1 4; 7 6 2 4; 1 5 6 4, what is the sum of the sub-rectangle from (1, 2) to (2, 2) inclusive?

  1. A. 37
  2. B. -9
  3. C. 1
  4. D. 3

Correct answer: D. 3

Sum = P[3][3] − P[1][3] − P[3][2] + P[1][2] = 3, where P is 1-based. Forgetting to add back the doubly-subtracted corner gives -9.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic