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

Best Time to Buy and Sell Stock

Motive logoMotive● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand report from a Motive staff-engineer onsite interview links the well-known Best Time to Buy and Sell Stock problem.

Our version is about 99% the same as the reported question.

You are given an integer array prices, where prices[i] is the price of one share on day i.

Choose at most one day to buy and one later day to sell. Return the maximum profit. If no profitable transaction exists, return 0.

Function

maxProfit(prices: int[]) → long

Examples

Example 1

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

Buy at price 1 and sell later at price 6 for profit 5.

Example 2

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

Every later price is lower, so the best choice is not to trade.

Example 3

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

Buy at a price of 0 and sell later at 4.

Constraints

  • 1 <= prices.length <= 200000
  • 0 <= prices[i] <= 10^9

More Motive Onsite Interview questions

  • Group AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Longest Equal-Zero-One SubarrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Maximum Weighted Sum After Left RotationsONSITE INTERVIEW · Seen Jan 2026
  • Valid Parenthesis StringPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Target Triplet SumONSITE INTERVIEW · Seen Dec 2025
  • Validate IP AddressONSITE INTERVIEW · Seen Dec 2025
  • Generate Palindromic PermutationsONSITE INTERVIEW · Seen Dec 2025
  • Minimum Swaps to Group Circular OnesONSITE INTERVIEW · Seen Dec 2025
  • Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Dec 2025
  • Search in Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025

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 StockBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Maximum Profit with Repeated Stock TradesPalantir · ONSITE INTERVIEWPalantir 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
All Motive interview questionsSee Motive hiring insights
Saved
class Solution {
    public long maxProfit(int[] prices) {
      // Write your code here.
    }

}
Checking account…