Find the Longest Consecutive Stay Under a Total Budget
Last updated: November 29, 2026
Quick Overview
Given nightly prices for a listing's upcoming calendar and a guest's total budget, find the longest run of consecutive nights whose total price does not exceed the budget. Return its length and starting index.
Airbnb
November 29, 20260
0
0 solved
Given nightly prices for a listing's upcoming calendar and a guest's total budget, find the longest run of consecutive nights whose total price does not exceed the budget. Return its length and starting index.
A flexible-dates search lets guests say how much they want to spend and asks how long they could stay. Airbnb phone screens often open with a warm-up like this to check that you reach for a linear sliding window instead of checking every pair of dates.
What the Interviewer Expects
- Note that prices are positive, which makes the window monotonic
- Use two pointers to expand and shrink the window in O(n)
- Track the best start and length, not just the length
- Handle a calendar where no single night fits the budget
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
- What breaks if some nights can have a negative adjustment, like a credit?
- How would you enforce a host's minimum stay of m nights?
- How would you answer many budget queries on the same calendar quickly?
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 is a list of positive nightly prices and an integer budget. Output is (length, start), where start is -1 if even one night is over budget. If two windows tie on length, returning the earliest is...
High-Level Design
Because every price is positive, adding a night to the right only increases the window sum, and removing from the left only decreases it. So for each right edge, the valid left edge only ever moves fo...