Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5 to [2..6]; add 1 to [2..7]; add 3 to [2..3]; add 6 to [1..3]. What is the final array?

  1. A. [0, 6, 15, 6, 6, 6, 1, 0]
  2. B. [0, 6, 15, 15, 6, 6, 6, 1]
  3. C. [1, 6, 15, 15, 6, 6, 6, 1]
  4. D. [0, 1, 6, 6, 6, 6, 15, 15]

Correct answer: B. [0, 6, 15, 15, 6, 6, 6, 1]

Mark +v at l and −v at r + 1 (not at r, which would drop the last index), then take the running sum: [0, 6, 15, 15, 6, 6, 6, 1].

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

Exams that ask this topic