Stream the Price Span for Each Day of a Seller's Price Feed

Last updated: November 29, 2026

Quick Overview

Build a class that receives a seller's daily price one day at a time and, for each new price, returns its span: the number of consecutive days ending today (including today) with price less than or equal to today's price.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Build a class that receives a seller's daily price one day at a time and, for each new price, returns its span: the number of consecutive days ending today (including today) with price less than or equal to today's price.

An Amazon technical screen question about online processing with a monotonic stack. The interviewer checks that you store (price, span) pairs so old days are folded together, keeping each call amortized O(1).

What the Interviewer Expects
  • Stores (price, span) pairs on a stack
  • Pops while top price <= today and accumulates spans
  • Pushes today's combined span
  • Argues amortized O(1) per call
  • Handles equal prices as part of the span
Key Topics to Cover
monotonic stack
online algorithms
amortized analysis
class design
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support queries for any past day's span.
  • Make it thread-safe for concurrent producers.
  • What if prices can be corrected after the fact?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Implement PriceSpanner with next(price) -> int. Calls come in chronological order, up to 10^5 calls. Span counts today and every consecutive prior day with a price <= today's....

High-Level Design

Keep a stack of (price, span) with strictly decreasing prices from bottom to top. When a new price arrives, start span at 1 and pop every entry with price <= today, adding its span, since all those da...


Submit Your Answer
Markdown supported