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

Best Time to Buy and Sell Stock

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report names the stock-profit task but does not say the limit on transactions, what to return when there is no profit, or the input limits. We use the usual single-transaction version. Our version is about 80% the same as the reported question.

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 one share and a later day to sell it. Return the maximum profit you can earn. If no profitable transaction exists, return 0.

Function

maxProfit(prices: int[]) → int

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 = [2,4,1,8]return = 7

Buying at price 1 and selling at price 8 beats the earlier profit of 2.

Constraints

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

More Headlands Technologies Phone Screen questions

  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Market Order BookPHONE SCREEN · Seen Jun 2026
  • Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
  • Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
  • Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
  • Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
  • CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
  • Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
  • Top Trade Events by NotionalPHONE SCREEN · Seen May 2024

Similar questions at other companies

  • Best Stock Trade with Buy and Sell DaysGoogle · PHONE SCREENGoogle interview questions
  • Can Place FlowersAgoda · PHONE SCREENAgoda interview questions
  • Maximum Later Checkpoint Altitude GainYellow.ai · PHONE SCREEN
  • Assign CookiesAmazon · PHONE SCREEN
  • Maximum Profit with Exactly One Stock TransactionRetool · PHONE SCREEN · ONSITE INTERVIEWRetool interview questions
  • Best Time to Buy and Sell Stock IIGroww · ONSITE INTERVIEW
  • Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Cheapest Round TripUber · ONSITE INTERVIEWUber interview questions
  • Best Time to Buy and Sell Stock IIPoint72 · PHONE SCREENPoint72 interview questions
  • Best Time to Buy and Sell StockJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int maxProfit(int[] prices) {
        // write your code here
    }
}
Checking account…