
Online Stock Span: Coding Problem Solution
Online Stock Span is a streaming-style problem that tests whether you can process data incrementally while keeping past information in a smart, compressed form. You are given daily stock prices one at a time, and for each new price, you must compute its span. The span of a stock’s price on a given day is defined as the number of consecutive days (including today) for which the price has been less than or equal to today’s price. For example, if today’s price is higher than yesterday’s and the day before yesterday, the span includes all of those days. If today’s price is lower than yesterday’s, the span is just 1. The key constraint is that this is an online problem. You cannot see future prices, and you must answer each query as it arrives. That’s what makes it different from classic array-based stock problems. Why a naive approach breaks down A straightforward approach is to look backward from today and count how many consecutive previous prices are smaller than or equal to. That works
Continue reading on Dev.to Tutorial
Opens in a new tab


