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
Coding & Algorithms
Software Engineer
SoFi
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
sliding window
two pointers
prefix sums
monotonicity
budgeting features
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
  • 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported

Related Questions