Reviewed by Aditya Kumar · Last reviewed 2026-03-24
Span = consecutive days (incl. today) with price ≤ today. Monotonic stack: store (price, span); for each price, pop smaller elements (sum their spans), push (price, total_span). Python: for p in prices: span=1; while stack and stack[-1][0]<=p: span+=stack.pop()[1];...
Span = consecutive days (incl. today) with price ≤ today. Monotonic stack: store (price, span); for each price, pop smaller elements (sum their spans), push (price, total_span). Python: for p in prices: span=1; while stack and stack[-1][0]<=p: span+=stack.pop()[1]; stack.append((p,span)); res.append(span). O(n)—each element pushed/popped at most once.
Red Flag: O(n²) brute force. Pro-Move: 'Monotonic stack—prices are processed in order; stack maintains 'candidate' left boundaries.'
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.