Stock span problem

Stack / Queues FAQs Hard
  • This problem, also known as the Stock Span Problem, is a fundamental concept often used in financial technologies, such as in trading algorithms
  • It computes how far back the price of a stock has been less than the current price, which can aid decision-making in stock trading
  • Thus, it's an essential part of the technology that powers applications and platforms for stock markets worldwide
  • On a conceptual level, it's closely related to the idea of a sliding window in which we monitor a specific parameter over a certain time - a technique broadly used in data streaming processing and time series analysis within the software industry

Given an array arr of size n, where each element arr[i] represents the stock price on day i. Calculate the span of stock prices for each day.


The span Si for a specific day i is defined as the maximum number of consecutive previous days (including the current day) for which the stock price was less than or equal to the price on day i.

Examples:

Input: n = 7, arr = [120, 100, 60, 80, 90, 110, 115]

Output: 1 1 1 2 3 5 6

Explanation:

Traversing the given input span:

120 is greater than or equal to 120 and there are no more elements behind it so the span is 1,

100 is greater than or equal to 100 and smaller than 120 so the span is 1,

60 is greater than or equal to 60 and smaller than 100 so the span is 1,

80 is greater than or equal to 60, 80 and smaller than 100 so the span is 2,

90 is greater than or equal to 60, 80, 90 and smaller than 100 so the span is 3,

110 is greater than or equal to 60, 80, 90, 100, 110 and smaller than 120 so the span is 5,

115 is greater than or equal to all previous elements and smaller than 120 so the span is 6.

Hence the output will be 1 1 1 2 3 5 6.

Input: n = 6, arr = [15, 13, 12, 14, 16, 20]

Output: 1 1 1 3 5 6

Explanation:

Traversing the given input span:

15 is greater than or equal to 15 and there are no more elements behind it so the span is 1,

13 is greater than or equal to 13 and smaller than 15 so the span is 1,

12 is greater than or equal to 12 and smaller than 13 so the span is 1,

14 is greater than or equal to 12, 14 and smaller than 15 so the span is 2,

16 is greater than or equal to 12, 14, 15, 16 and smaller than 20 so the span is 4,

20 is greater than or equal to all previous elements so the span is 6.

Hence the output will be 1 1 1 2 4 6.

Input: n = 8, arr = [30, 20, 25, 28, 27, 29, 35, 40]

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ arr[i] ≤ 105

Hints

  • A brute-force approach would iterate over all previous days for each i and check how many consecutive days satisfy the condition, leading to O(n²) complexity, which is inefficient for large n.
  • Use a Monotonic Decreasing Stack to efficiently find the previous smaller element. Maintain a stack of indices where prices are in decreasing order. For each day i, pop elements from the stack until a smaller price is found, and calculate the span as: S i =i−previous smaller index Push the current index onto the stack.

Company Tags

Riot Games Square Reddit Target Databricks Instacart Walmart Epic Systems Electronic Arts Red Hat Rakuten Salesforce Zynga Goldman Sachs Western Digital Roche Rockstar Games Deloitte Chewy eBay Texas Instruments Dropbox Lyft Boston Consulting Group Medtronic Google Microsoft Amazon Meta Apple Netflix Adobe