Maximum Stock Profit with One Transaction
Updated
Problem statement
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. Return the maximum profit from at most one such transaction. You may choose no transaction, which gives a profit of zero.
Function
maxProfit(prices: int[]) → intExamples
Example 1
prices = [2,100,150,120]return = 148Buy at price 2 on day 0 and sell at price 150 on day 2. The profit is 150 - 2 = 148.
Example 2
prices = [7,6,4,3,1]return = 0Every later price is lower than every earlier price. Choosing no transaction gives the best profit, 0.
Constraints
1 <= prices.length <= 100000.0 <= prices[i] <= 1000000000.
More Expedia Online Assessment questions
- Balanced NumbersOA · Seen Aug 2026
- REST API: Discounted PriceOA · Seen Aug 2026
- Maximum Team SizeOA · Seen Mar 2026
- Per-Domain Request LimiterOA · Seen Oct 2025
- Count Equal-Frequency SubsequencesOA · Seen Mar 2025
- Count Increasing Stock PeriodsOA · Seen Mar 2025
- Get Minimum StepsOA · Seen Oct 2024
- Rank Songs by PopularityOA · Seen Oct 2024
- Calculate the SumOA · Seen Oct 2024
- Get Maximum EfficiencyOA · Seen Oct 2024
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Minimum Project Bid CostNVIDIA · OANVIDIA interview questions
- Maximum Profit with Exactly One Stock TransactionRetool · PHONE SCREEN · ONSITE INTERVIEWRetool interview questions
- Minimum AmountBelvedere Trading · OA
- Maximum Income from Asset TradingJefferies · OA
- Best Time to Buy and Sell Stock IIGroww · ONSITE INTERVIEW
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions