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

Best Time to Buy and Sell Stock

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The round is not named in the review. It lists three technical questions and several behavioral ones from a Microsoft software engineering internship process that began with online assessments. A review lists a stock buy-and-sell question by name only, with no input or rules.

The review does not say how many trades are allowed. Here you make at most one trade (buy, then sell on a later day) and return the largest profit, or 0 if no trade gains. There are 1 to 10^4 daily prices from 0 to 10^4.

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

You are given an array prices, where prices[i] is the price of a stock on day i.

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

Function

maxProfit(prices: int[]) → int

Examples

Example 1

prices = [8,3,6,2,9,4]return = 7

Buy on day 3 at price 2 and sell on day 4 at price 9 for a profit of 7. No other pair of days does better: buying at 3 and selling at 9 gives 6.

Example 2

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

The price only falls, so every sale would lose money. Not trading gives 0.

Example 3

prices = [5]return = 0

With one day there is no later day to sell on, so the answer is 0.

Constraints

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

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 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 StockAmazon · ONSITE INTERVIEW
  • Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle 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

Interview reports that mention this question

  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int maxProfit(int[] prices) {
        // Write your code here.
    }
}
Checking account…