Days Until a Cheaper Night on a Listing's Price Calendar
Last updated: November 29, 2026
Quick Overview
Given a listing's nightly prices in date order, return for each night how many nights later a strictly cheaper night occurs, or 0 if none does. Guests use this kind of hint to decide whether waiting a few days saves money.
Airbnb
November 29, 20260
0
0 solved
Given a listing's nightly prices in date order, return for each night how many nights later a strictly cheaper night occurs, or 0 if none does. Guests use this kind of hint to decide whether waiting a few days saves money.
Airbnb's flexible date tools can nudge guests toward cheaper nearby dates. As an online assessment item it checks whether you recognize the next-smaller-element pattern and use a monotonic stack instead of a nested loop.
What the Interviewer Expects
- Identify the brute force O(n^2) and why it is too slow for long calendars
- Use a stack of indexes with non-decreasing prices
- Resolve waiting nights when a cheaper price arrives
- Handle equal prices correctly (strictly cheaper only)
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 cheaper price itself instead of the distance?
- How would you limit the search to the next 14 nights?
- How would you find the previous cheaper night instead?
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: list of prices. Output: list of the same length where out[i] = j - i for the first j > i with prices[j] < prices[i], else 0....
High-Level Design
Scan left to right with a stack of indexes still waiting for a cheaper night. The stack's prices are non-decreasing from bottom to top, because any higher price below a lower one would already have be...