Best Time to Buy and Sell Stock
Updated
Problem statement
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[]) → intExamples
Example 1
prices = [7,1,5,3,6,4]return = 5Buy at price 1 and sell later at price 6 for profit 5.
Example 2
prices = [7,6,4,3,1]return = 0Every later price is lower, so the best choice is not to trade.
Example 3
prices = [2,4,1,8]return = 7Buying 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