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

Best Time to Buy and Sell Stock

Oracle logoOracle● EasyFULLTIMEPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The judged core task matches the visible source at about 99%.

You are given an 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.

Example 2

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

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

Constraints

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

More Oracle problems

  • Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
  • Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
  • Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public long maxProfit(int[] prices) {
      // Write your code here.
    }

}
Checking account…