Quick Overview

Given a stock's daily prices, return the largest profit from one purchase followed by one sale on a strictly later day, or zero when no trade makes money. It tests efficient reasoning about ordered buy and sell pairs in arrays of up to a hundred thousand prices.

Maximum Profit from One Stock Purchase and One Later Sale

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given `prices`, where `prices[i]` is the price of one share of a stock on day `i`. You may make at most one trade: buy one share on some day and sell it on a strictly later day. Return the largest profit you can make. If no trade makes a positive profit, return `0`. ### Function Signature ```python def max_single_trade_profit(prices: list[int]) -> int: ... ``` ### Rules - The sell day must come strictly after the buy day. - Making no trade is allowed and yields a profit of `0`, so the answer is never negative. - Only one share is bought and sold, and there are no fees. ### Constraints - `1 <= len(prices) <= 10^5` - `0 <= prices[i] <= 10^4` - The output is a single integer. ### Examples #### Example 1 ```text Input: prices = [8, 2, 6, 4, 9, 3] Output: 7 ``` Buy on day 1 at price 2 and sell on day 4 at price 9. #### Example 2 ```text Input: prices = [9, 7, 4, 2] Output: 0 ``` Prices only fall, so every trade loses money and making no trade is best. #### Example 3 ```text Input: prices = [3, 5, 1, 4] Output: 3 ``` Buying at 1 on day 2 and selling at 4 on day 3 gives 3, which beats buying at 3 and selling at 5 for a profit of 2.

Overview: Given a stock's daily prices, return the largest profit from one purchase followed by one sale on a strictly later day, or zero when no trade makes money. It tests efficient reasoning about ordered buy and sell pairs in arrays of up to a hundred thousand prices.

Read the full Amazon Software Engineer interview experience this question came from

You are given an array `prices`, where `prices[i]` is the price of one share of a stock on day `i` (days are numbered from 0). You may make at most one trade: buy one share on some day and sell that share on a strictly later day. Return the largest profit you can make. If no trade gives a positive profit, return `0`. Implement `max_single_trade_profit(prices)`, which returns the answer as a single integer. ### Rules - The sell day must come strictly after the buy day. - Making no trade is allowed and gives a profit of `0`, so the answer is never negative. - Only one share is bought and sold, and there are no fees. - Every price and every possible answer lies between 0 and 10^4, so all values fit in a signed 32-bit integer. ### Example 1 ```text Input: prices = [8, 2, 6, 4, 9, 3] Output: 7 ``` Buy on day 1 at price 2 and sell on day 4 at price 9. ### Example 2 ```text Input: prices = [3, 5, 1, 4] Output: 3 ``` Buying at 1 on day 2 and selling at 4 on day 3 gives 3, which beats buying at 3 and selling at 5 for a profit of 2. ### Constraints - `1 <= len(prices) <= 10^5` - `0 <= prices[i] <= 10^4` - The output is a single integer.

Constraints

  • 1 <= len(prices) <= 10^5
  • 0 <= prices[i] <= 10^4
  • The output is a single integer.
  • The answer is between 0 and 10^4 and fits in a signed 32-bit integer.

Examples

Input: ([8, 2, 6, 4, 9, 3],)

Expected Output: 7

Explanation: Source example 1: buy at 2 on day 1 and sell at 9 on day 4 for a profit of 7.

Input: ([9, 7, 4, 2],)

Expected Output: 0

Explanation: Source example 2: prices only fall, so no trade is profitable and the answer is 0.

Hints

  1. Making no trade is always allowed, so the answer is at least 0.
  2. The sell day must come after the buy day, so the overall lowest and highest prices form a valid trade only when the lowest comes first. Example 2 shows the best trade can skip the highest price.
  3. With up to 10^5 days, comparing every pair of days takes about 5 * 10^9 checks; look for a way to handle each day once.

Maximum Profit From One Stock Trade

mediumAmazonSoftware Engineer

You are given an array prices, where prices[i] is the price of one share of a stock on day i (days are numbered from 0). You may make at most one trade: buy one share on some day and sell that share on a strictly later day. Return the largest profit you can make. If no trade gives a positive profit, return 0.

Implement max_single_trade_profit(prices), which returns the answer as a single integer.

Rules

  • The sell day must come strictly after the buy day.
  • Making no trade is allowed and gives a profit of 0, so the answer is never negative.
  • Only one share is bought and sold, and there are no fees.
  • Every price and every possible answer lies between 0 and 10^4, so all values fit in a signed 32-bit integer.

Example 1

Input: prices = [8, 2, 6, 4, 9, 3]
Output: 7

Buy on day 1 at price 2 and sell on day 4 at price 9.

Example 2

Input: prices = [3, 5, 1, 4]
Output: 3

Buying at 1 on day 2 and selling at 4 on day 3 gives 3, which beats buying at 3 and selling at 5 for a profit of 2.

Constraints

  • 1 <= len(prices) <= 10^5
  • 0 <= prices[i] <= 10^4
  • The output is a single integer.

Examples

Example 1
Input
([8, 2, 6, 4, 9, 3],)
Output
7
Notes
Source example 1: buy at 2 on day 1 and sell at 9 on day 4 for a profit of 7.
Example 2
Input
([9, 7, 4, 2],)
Output
0
Notes
Source example 2: prices only fall, so no trade is profitable and the answer is 0.

Constraints

  • •1 <= len(prices) <= 10^5
  • •0 <= prices[i] <= 10^4
  • •The output is a single integer.
  • •The answer is between 0 and 10^4 and fits in a signed 32-bit integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_single_trade_profit(prices: list[int]) -> int:
Sample call
max_single_trade_profit([8, 2, 6, 4, 9, 3])
Language
Case 1
Input
([8, 2, 6, 4, 9, 3],)
Expected
7
Case 2
Input
([9, 7, 4, 2],)
Expected
0

Browse More Questions

Show the approach

Approach

Scan the days from left to right while keeping two values: the lowest price on any day already passed, and the best profit found so far. Any trade that sells on day i earns the most when it buys at the lowest earlier price, so on day i the only candidate worth checking is prices[i] minus that running minimum. The profit check runs before prices[i] joins the running minimum, which keeps the buy day strictly before the sell day. After day i the best profit equals the largest profit of any trade that sells on or before day i, so after the last day it is the answer. The best profit starts at 0, which stands for making no trade, so flat or falling prices and a single-day list all return 0. A later new minimum only replaces the running minimum and never lowers the stored best profit. Every profit lies between 0 and 10^4, so 32-bit integers suffice in every language.

Time complexity:
O(n)
Space complexity:
O(1)