Category Archives: Stack

The stock span problem

The stock span

The stock span is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days. The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.

For example,
If an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.

Algorithm:
  • We will use stack to store last highest price found till current index
  • Now create stack and push 0 in it
  • Create prices array S to hold Si and put S[0] = 1, because it will be always true
  • Iterate given array from 1 to n (here n = array.length)
    • If prices[stack.top] <= prices[i] then
      • stack.pop
    • Repeat till stack is not empty or above condition does not break.
    • Now at this point there could be two possible states of stack
      • Case 1: When stack is empty that means, all previous prices was smaller than current prices
      • Case 2: When stack is not empty that means, there is atleast one price which higher than current prices.
    • If stack is empty then
      • S[i] = i + 1
    • Else
      • S[i] = i – stack.top
    • stack.push(i)

Video

Latest Source Code:
Github: StockSpan.java


Output:

100 - 1
80 - 1
60 - 1
70 - 2
60 - 1
75 - 4
85 - 6