Days Until a Product's Price Rises Above Today's Price

Last updated: November 29, 2026

Quick Overview

Given a product's daily prices on Amazon, return a list where entry i is the number of days you must wait after day i for a strictly higher price, or 0 if no later day is higher.

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

0

0

0 solved


Given a product's daily prices on Amazon, return a list where entry i is the number of days you must wait after day i for a strictly higher price, or 0 if no later day is higher.

The entry-level monotonic stack question in Amazon phone screens, framed around the price history that deal watchers and sellers track. The interviewer wants the decreasing stack of indices and an amortized O(n) argument.

What the Interviewer Expects
  • Explains why the O(n^2) scan is too slow
  • Keeps a stack of indices with decreasing prices
  • Resolves waiting days when a higher price arrives
  • Leaves 0 for unresolved indices
  • Gives the amortized O(n) argument
Key Topics to Cover
monotonic stack
next greater element
amortized analysis
arrays
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
  • Return the next higher price itself rather than the wait.
  • Find the previous higher price for each day instead.
  • What if prices arrive as a stream and you must answer as soon as possible?
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

Input: list of prices (integers or floats), length up to 10^5. Output: list wait of the same length. Strictly higher only; equal prices do not count....

High-Level Design

Scan left to right holding a stack of day indices whose next higher price has not been found; their prices are non-increasing from bottom to top. When day i arrives, pop every index j with price[j] < ...


Submit Your Answer
Markdown supported