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

Best Time to Buy and Sell Stock

Amazon logoAmazon● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the first onsite round of an Amazon full-time interview, the candidate was asked the classic stock buy and sell question and wrote the code on paper. The report names the question and nothing else.

It does not give the rules, examples, or limits. Here, you make at most one trade, you must sell on a later day than you buy, you return 0 when no trade makes money, and there are up to 10^5 prices from 0 to 10^4.

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

You are given an array prices. The value prices[i] is the price of one share of a stock on day i.

You may make at most one trade. A trade means you buy one share on one day and sell it on a later day. Return the largest profit you can make. If no trade makes money, do not trade and return 0.

Function

maxProfit(prices: int[]) → int

Examples

Example 1

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

Buy on day 0 at price 3 and sell on day 1 at price 8. The profit is 8 - 3 = 5.

Buying at the cheaper prices 2 or 1 gives less: the best later prices are 6 and 4, so those trades earn 4 and 3.

Example 2

prices = [9,7,4,1]return = 0

The price goes down every day, so every trade loses money. Not trading is best, and the answer is 0.

Example 3

prices = [5,5,2,9]return = 7

Buy on day 2 at price 2 and sell on day 3 at price 9. The profit is 9 - 2 = 7.

Buying at 5 on day 0 or day 1 and selling at 9 on day 3 earns only 9 - 5 = 4, which is less.

Constraints

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Best Time to Buy and Sell StockKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Maximum SubarrayMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum SubarraySquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
  • Best Time to Buy and Sell StockZoox · ONSITE INTERVIEW
  • Maximum Subarray SumSafe Security · ONSITE INTERVIEW
  • Pascal's TriangleEricsson · ONSITE INTERVIEWEricsson interview questions
  • 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
  • Maximum Profit with Repeated Stock TradesPalantir · ONSITE INTERVIEWPalantir interview questions

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
  • Amazon SDE II (L5) Interview Questions: 5 Experiences
See Amazon hiring insights
Saved
class Solution {
    public int maxProfit(int[] prices) {
        // Write your code here.
    }
}
Checking account…