Maximum Profit From a Single Buy Followed by a Later Sell

Read the full interview experience this question came from →

Quick Overview

An Amazon coding problem: given daily stock prices, find the maximum profit from buying once and selling on a later day, or zero if no trade makes money. It tests linear-time reasoning over an array and careful handling of falling prices and single-day input.

Maximum Profit From a Single Buy Followed by a Later Sell

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given the price of one stock on each of a sequence of days: `prices[i]` is the price on day `i`. You may buy one share on some day and sell it on a strictly later day, at most once. Return the largest profit you can make. If no buy-then-sell pair makes money, you do not trade and the profit is 0. ### Function Signature ```python def max_single_trade_profit(prices: list[int]) -> int: ``` ### Rules - Buying on day `i` and selling on day `j` earns `prices[j] - prices[i]`, and requires `i < j`. You cannot sell before you buy. - At most one buy and one sell in total. Not trading is always allowed, so the answer is never negative. - Return only the profit, which is unique even when several pairs of days achieve it. ### Constraints - `1 <= len(prices) <= 100000` - `0 <= prices[i] <= 10000` - The answer is an integer between 0 and 10000. ### Examples **Example 1** ```text Input: prices = [9, 4, 6, 3, 8, 5] Output: 5 ``` Buy on day 3 at price 3 and sell on day 4 at price 8. Buying at 4 and selling at 8 would earn only 4. **Example 2** ```text Input: prices = [10, 8, 5, 5, 1] Output: 0 ``` The price never rises from any day to a later day, so the best choice is not to trade. **Example 3** ```text Input: prices = [7] Output: 0 ``` With a single day there is no later day to sell on.

Overview: An Amazon coding problem: given daily stock prices, find the maximum profit from buying once and selling on a later day, or zero if no trade makes money. It tests linear-time reasoning over an array and careful handling of falling prices and single-day input.

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

Amazon logo
Amazon
Apr 6, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

You are given the price of one stock on each of a sequence of days: prices[i] is the price on day i. You may buy one share on some day and sell it on a strictly later day, at most once. Return the largest profit you can make. If no buy-then-sell pair makes money, you do not trade and the profit is 0.

Function Signature

def max_single_trade_profit(prices: list[int]) -> int:

Rules

  • Buying on day i and selling on day j earns prices[j] - prices[i] , and requires i < j . You cannot sell before you buy.
  • At most one buy and one sell in total. Not trading is always allowed, so the answer is never negative.
  • Return only the profit, which is unique even when several pairs of days achieve it.

Constraints

  • 1 <= len(prices) <= 100000
  • 0 <= prices[i] <= 10000
  • The answer is an integer between 0 and 10000.

Examples

Example 1

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

Buy on day 3 at price 3 and sell on day 4 at price 8. Buying at 4 and selling at 8 would earn only 4.

Example 2

Input: prices = [10, 8, 5, 5, 1]
Output: 0

The price never rises from any day to a later day, so the best choice is not to trade.

Example 3

Input: prices = [7]
Output: 0

With a single day there is no later day to sell on.