Data Structures & AlgorithmsVery hard

A zero-sum subarray counter keeps a map of prefix sums. At each index the current prefix is inserted into the map BEFORE the lookup. What goes wrong?

  1. A. Negative prefix sums are never found in the map.
  2. B. Nothing; the lookup order does not change the answer.
  3. C. The map grows to the wrong size, so the program runs out of memory.
  4. D. Each prefix matches itself, so empty ranges are counted and the answer is too large.

Correct answer: D. Each prefix matches itself, so empty ranges are counted and the answer is too large.

A prefix sum inserted before its lookup finds itself and counts an empty subarray at every index. The lookup must come first so that only earlier prefixes are matched.

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

Exams that ask this topic