Days Until a Lower Price for Each Product in a Price Feed
Last updated: November 29, 2026
Quick Overview
Given a product's daily prices, return for each day how many days you would wait until a strictly lower price appears, or 0 if it never does.
Walmart Labs
November 29, 20260
0
0 solved
Given a product's daily prices, return for each day how many days you would wait until a strictly lower price appears, or 0 if it never does.
Price drop alerts and Rollback tracking on Walmart.com depend on knowing when a price next falls. In the Walmart Labs online assessment this checks whether you replace an O(n^2) scan with a monotonic stack.
What the Interviewer Expects
- Point out the quadratic brute force first
- Use a stack of indices whose prices are not yet resolved
- Pop while the current price is strictly lower
- Handle equal prices according to the strict rule
- Prove linear time by counting pushes and pops
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the next lower price itself, not the wait?
- How would you answer this for a circular 52-week price cycle?
- How would you process a stream where you cannot store the whole history?
- What if you need the next price that is at least 10 percent lower?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a list of integer prices in cents, one per day. Output: a list of the same length where entry i is the gap in days to the first later day with a strictly lower price, or 0. Equal prices do not ...
High-Level Design
Brute force checks every later day and costs O(n^2). Instead, keep a stack of days still waiting for a lower price. Their prices are non-decreasing from bottom to top, because when a lower price arriv...