FastPrepBest Time to Buy and Sell Stock II
Problems/Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names the stock1 and stock2 variants in its second onsite round. This exercise brings back the unlimited-transactions Stock II rules and limits from that problem. The function name is our choice, since the original interview code was not reported. Our version is about 90% the same as the reported question.

prices[i] is the price of one share on day i. Starting with no shares, choose purchases and sales to maximize your total realized profit.

You may complete any number of transactions, but you may hold at most one share at a time. Buying and selling on the same day is allowed as long as you never hold more than one share. There are no transaction fees, cooldown days or limit on the number of completed transactions.

Return the maximum total profit after all days, ending with no share held. You may make no trades and earn 0. This is the unlimited-transactions version; do not restrict the answer to one buy and one sell. Do not modify prices.

Function

maxProfitII(prices: int[]) → int

Examples

Example 1

prices = [7,1,5,3,6,4]return = 7

Buy at 1 and sell at 5 for 4, then buy at 3 and sell at 6 for 3. Two transactions yield total 7.

Example 2

prices = [1,2,3,4,5]return = 4

One transaction from 1 to 5 yields 4. Splitting this increasing run yields the same total.

Example 3

prices = [7,6,4,3,1]return = 0

No positive realized profit is possible, so make no trades and return 0.

Constraints

  • 1 <= prices.length <= 30000.
  • 0 <= prices[i] <= 10000.
  • The result is at most 150000000, so all profit calculations fit in a signed 32-bit integer.

More Headlands Technologies interview questions

  • As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
  • Triggered Order Event FilterONSITE INTERVIEW · Seen Oct 2024
  • Best Time to Buy and Sell StockPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2024
  • House RobberONSITE INTERVIEW · Seen Apr 2024
  • Merge IntervalsONSITE INTERVIEW · Seen Apr 2024
  • Net Position Calculation from API SnapshotsONSITE INTERVIEW · Seen Apr 2024
  • Disaster RecoveryOA · Seen Nov 2026
  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Array Interval SumsOA · Seen Oct 2026
  • Job Runner Chain ReportsOA · Seen Oct 2026

Similar questions at other companies

  • Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • 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
  • First Five Increasing Temperature RangesAmazon · ONSITE INTERVIEW
  • Return a Minimum-Coin Change BreakdownDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Most Transactions Kept Without Going NegativeDocuSign · ONSITE INTERVIEWDocuSign interview questions
  • Fewest Jumps to the Last IndexC3.ai · ONSITE INTERVIEWC3.ai interview questions
  • Jump GameBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int maxProfitII(int[] prices) {
        // write your code here
    }
}
Checking account…