MEDIUMasked at 2 companies

Online Stock Span

A medium-tier problem at 67% community acceptance, tagged with Stack, Design, Monotonic Stack. Reported in interviews at INDmoney and 1 others.

Founder's read

Online Stock Span appears in live assessments at Microsoft and INDmoney, and it's a tricky design problem that catches candidates off guard. You're building a real-time system that computes span values as stock prices stream in, where span means the number of consecutive days the price was less than or equal to today's price. The naive approach is O(n) per query, which times out. The trick is a monotonic stack that tracks previous days you actually care about. If you hit this during your OA and don't see the pattern immediately, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
2
Difficulty
MEDIUM
Acceptance
67%

Companies that ask "Online Stock Span"

If this hits your live OA

Online Stock Span is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The core insight is that you don't need to scan backwards through every single day. A monotonic decreasing stack stores indices of days whose prices matter for future span calculations. When a new price comes in, pop all days with lower prices, then the span is simply the difference between today's index and the index at the top of the stack. This is O(1) amortized per call because each day enters and exits the stack exactly once. Common pitfall: trying to iterate backwards through days manually, or storing prices instead of indices. Another trap is off-by-one errors when computing the span after popping the stack. If you blank on the monotonic stack pattern during the assessment, StealthCoder handles the implementation details so you keep moving.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Online Stock Span recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Online Stock Span interview FAQ

Why does the brute-force backwards loop get rejected?+

Scanning from the current day backwards to find the span is O(n) per query. With large data streams, you hit time limits. A monotonic stack reduces this to O(1) amortized by remembering which historical prices are relevant for future lookups.

Is this still asked at Microsoft and other companies?+

Yes. It's a design and data structure problem that tests both stack understanding and real-time algorithmic thinking. Companies like Microsoft use it to evaluate how you handle streaming data under performance constraints.

What's the trick with the monotonic stack here?+

Maintain a stack of indices in strictly decreasing order of price. When a new price arrives, pop indices with lower or equal prices, then the span is the current index minus the new stack top. This avoids redundant historical lookups.

How does this relate to other monotonic stack problems?+

Like Next Greater Element or Largest Rectangle, it uses a stack to efficiently query historical information without rescanning. The pattern is: maintain monotonic order, pop to find the previous relevant element, compute the result from the distance.

What are the most common bugs during implementation?+

Storing prices instead of indices (makes span calculation wrong), forgetting to handle the empty stack case (when no previous day qualifies, span is the full index plus one), and off-by-one errors in the span formula.

Want the actual problem statement? View "Online Stock Span" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.