Best Time to Buy and Sell Stock
Updated
Problem statement
You are given an integer array prices, where prices[i] is the price of one share on day i.
Choose at most one day to buy and one later day to sell. Return the maximum profit. If no profitable transaction exists, return 0.
Function
maxProfit(prices: int[]) → longExamples
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 = [3,3,5,0,0,3,1,4]return = 4Buy at a price of 0 and sell later at 4.
Constraints
1 <= prices.length <= 2000000 <= prices[i] <= 10^9
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · 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 StockJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Best Time to Buy and Sell StockZoox · ONSITE INTERVIEW
- Best Time to Buy and Sell StockMotive · ONSITE INTERVIEWMotive interview questions
- Maximum Profit with Repeated Stock TradesPalantir · ONSITE INTERVIEWPalantir interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- Minimum Taps to Water a GardenThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN