Maximum Profit with Repeated Stock Trades
Updated
Problem statement
You are given an integer array prices, where prices[i] is the price of one stock on day i.
For this exercise, assume you start without stock and can buy and sell as many times as you want. You may hold at most one share at a time, and you must sell a share before buying another. Buying and selling on the same day is allowed.
There are no transaction fees, cooldown periods, short sales, or limits on the cash available for a purchase. Finish without stock. You may also make no trades.
Return the maximum total profit. Each sale contributes its price minus the price paid for that share.
Function
maxTradingProfit(prices: int[]) → intExamples
Example 1
prices = [2,5,1,6]return = 8Buy at 2 and sell at 5 for a profit of 3. Then buy at 1 and sell at 6 for a profit of 5. The total is 8.
Example 2
prices = [9,7,4]return = 0Every later price is lower. Making no trades gives the best profit, 0.
Example 3
prices = [1,1,3,3,2,5]return = 5Buy at 1 and sell at 3 for a profit of 2. Then buy at 2 and sell at 5 for a profit of 3. Equal prices add no profit, so the total is 5.
Constraints
1 <= prices.length <= 30000.0 <= prices[i] <= 10000.
More Palantir interview questions
- Map and Accumulate Learning QueriesONSITE INTERVIEW · Seen Oct 2026
- Relational Learning QueriesONSITE INTERVIEW · Seen Apr 2025
- Find Unmet Users With Shared InterestsONSITE INTERVIEW · Seen Mar 2025
- Minimum Calls for Repeated API LookupsONSITE INTERVIEW · Seen Oct 2022
- Find Cookable RecipesONSITE INTERVIEW · Seen Nov 2019
- Simulate Multiplayer WarONSITE INTERVIEW · Seen Nov 2019
- Trace Terminal Company OwnershipONSITE INTERVIEW · Seen Nov 2019
- Query Words Within K PositionsPHONE SCREEN · Seen Nov 2026
- Basic Item DiscountsOA · Seen Nov 2026
- Optimize Discount Selection Under a Point BudgetOA · Seen Nov 2026
Similar questions at other companies
- Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell StockZoox · ONSITE INTERVIEW
- Best Time to Buy and Sell StockMotive · ONSITE INTERVIEWMotive interview questions
- Best Time to Buy and Sell StockBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimum Taps to Water a GardenThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Maximum Stock Profit with Unlimited TransactionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Maximum Stock Profit with Transaction LimitsMeta · ONSITE INTERVIEWMeta interview questions
- Jump Game IITripjack · ONSITE INTERVIEWTripjack interview questions
- First Five Increasing Temperature RangesAmazon · ONSITE INTERVIEW
Interview reports that mention this question
Palantir interview experiences
- Palantir FDSE New Grad OA Questions: 6 Experiences (Part 2 of 2)
- Palantir FDSE New Grad Interviews: 5 Experiences (Part 1 of 2)
- Palantir FDSE Intern OA and Interviews: 3 Experiences
- Palantir FDSE Karat and Learning: 7 Experiences (Part 2 of 2)
- Palantir FDSE OA and Interviews: 16 Experiences (Part 1 of 2)