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?
- A. 17
- B. 16
- C. 9
- 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
- Problem: Five customers with service times 5, 3, 8, 2, 4 (in arrival order) are served by two tellers. Each customer goes to whichever…
- Problem: How many ordered pairs (a, b) of positive integers satisfy a × b = 36 with a ≤ 100 and b ≤ 100?
- Problem: How many non-negative integer solutions (x, y, z) satisfy x + y + z = 10?
- Problem: How many integer solutions (x, y, z) with x, y, z ≥ 1 satisfy x + y + z = 10?
- Problem: A derangement is a permutation with no fixed point. How many derangements of 5 objects exist?
- Problem: How many binary trees (distinct shapes, ignoring labels) can be formed with 6 nodes?