Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: The stock span of a day is the number of consecutive days (including today) up to and including today with a price at most today's price. For prices [100, 80, 60, 70, 60, 75, 85], what is the sum of all spans?

  1. A. 17
  2. B. 16
  3. C. 9
  4. D. 28

Correct answer: B. 16

A monotonic stack gives spans 1, 1, 1, 2, 1, 4, 6, summing to 16. Counting every earlier day with a price ≤ today's, not just the consecutive run, gives 17, excluding today from each span gives 9, and 28 assumes every span reaches back to day 1.

More Coding Interview Problems questions

Exams that ask this topic