Most Transactions Kept Without Going Negative
Updated
Problem statement
An account starts with a balance of 0. You are given a list of transactions in time order. A positive value is a deposit, a negative value is a withdrawal, and 0 changes nothing.
You may skip any transactions you like. The ones you keep are applied in their original order. After each kept transaction, the balance must be 0 or more.
Return the largest number of transactions you can keep. Keeping none is allowed, so the answer is at least 0.
Function
longestSafeTransactions(transactions: int[]) → intExamples
Example 1
transactions = [3,-4,2,-1,-2]return = 4Keep 3, skip -4, then keep 2, -1 and -2. The balance goes 3, 5, 4, 2 and never drops below 0. Keeping -4 after 3 would make the balance -1, so at most 4 transactions fit. The answer is 4.
Example 2
transactions = [2,-2,-1,-1]return = 3Keeping 2 and -2 first leaves a balance of 0, and then neither -1 fits, which gives only 2. Skipping -2 instead and keeping 2, -1, -1 gives balances 2, 1, 0, so 3 transactions fit. The answer is 3.
Example 3
transactions = [-5,-1]return = 0The balance starts at 0, so any withdrawal kept first would make it negative. No transaction can be kept, and the answer is 0.
Constraints
1 <= transactions.length <= 10^4-10^4 <= transactions[i] <= 10^4
More DocuSign Onsite Interview questions
- Count Binary SubstringsONSITE INTERVIEW · Seen Sep 2026
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Compress Consecutive Integer RangesONSITE INTERVIEW · Seen Aug 2026
- JSON Schema Object ValidatorONSITE INTERVIEW · Seen Aug 2026
- Three-Part Splits With a Light MiddleONSITE INTERVIEW · Seen May 2025
- Combine Two Sorted Lists Into OneONSITE INTERVIEW · Seen Apr 2022
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Paired Words That Share a SubstringONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Order of a Linked ListONSITE INTERVIEW · Seen Feb 2020
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimize Total Weight After HalvingAmazon · ONSITE INTERVIEW
- Maximum Stock Profit with Unlimited TransactionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Jump Game IIOracle · ONSITE INTERVIEWOracle interview questions
- Maximum Stock Profit with Transaction LimitsMeta · ONSITE INTERVIEWMeta interview questions
- Jump Game IITripjack · ONSITE INTERVIEWTripjack interview questions
- Minimum Workers with Job AssignmentsLyft · ONSITE INTERVIEWLyft interview questions
- Return a Minimum-Coin Change BreakdownDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Best Time to Buy and Sell Stock IIHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions