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 one 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 a profit of 5.
Example 2
prices = [7,6,4,3,1]return = 0Every later price is lower, so no profitable transaction exists.
Example 3
prices = [2,4,1]return = 2Buy on the first day at price 2 and sell on the second day at price 4.
Constraints
1 <= prices.length <= 2000000 <= prices[i] <= 10^9
More JP Morgan Chase interview questions
- Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
- Maximize Binary XOR with a Set-Bit LimitPHONE SCREEN · Seen Oct 2026
- Top K Frequent Values Across Key-Partitioned ShardsPHONE SCREEN · Seen Sep 2026
- All Index Pairs Sum to KPHONE SCREEN · Seen Aug 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
Similar questions at other companies
- Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell StockBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
- Jump Game IINavi · PHONE SCREEN
- Best Stock Profit With a Sell FeeKaseya · PHONE SCREEN
- Stock Profit With One, Unlimited, or Two TradesAdobe · PHONE SCREENAdobe interview questions
- Return a Minimum-Coin Change BreakdownDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Relevant Text SpanHebbia · PHONE SCREEN
JP Morgan Chase interview experiences
- JPMorgan Chase Experienced SWE Java Interview: 1 Experience
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)