Longest Run of Days Under a Spending Budget
Last updated: November 29, 2026
Quick Overview
Given a member's non-negative daily spending amounts in cents and a budget, find the longest contiguous run of days whose total spend stays within the budget. Return its length and start and end indices.
SoFi
November 29, 20260
0
0 solved
Given a member's non-negative daily spending amounts in cents and a budget, find the longest contiguous run of days whose total spend stays within the budget. Return its length and start and end indices.
SoFi's money app includes budgeting and spending insights, and a feature like 'your longest stretch under budget' maps directly onto a sliding window. The interviewer is checking that you recognize when a two pointer window is valid (non-negative values) and can explain why it fails with negative values.
What the Interviewer Expects
- Uses a single pass with left and right pointers, not O(n^2) prefix pairs
- States that the window works because amounts are non-negative
- Shrinks the window while the total exceeds the budget
- Returns a sensible sentinel when no single day fits
- Explains what changes if refunds create negative days
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 solve it if refunds make some days negative?
- How would you answer the same question for many budgets at once?
- What if you need the run with the highest total that still fits the budget?
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 daily spend in integer cents (all >= 0) and a budget. Output: (length, start, end) of the longest window with sum <= budget, earliest such window on ties, and (0, -1, -1) if every s...
High-Level Design
Because every value is non-negative, extending the window can only increase the sum and shrinking it can only decrease it. That monotonicity means once a window is over budget, any larger window with ...