Best Time to Buy and Sell Stock

Find the maximum profit from buying and selling a stock once

You are given an array prices where prices[i] is the price of a given stock on the ith day. You may buy on one day and sell on another day later. Return the maximum profit you can achieve. If no profit is possible, return 0.

Difficultyintermediate

R — READ

R

READ

Question

What constraints apply? Can I short-sell? Must I hold before selling?

Focus

Buy on one day, sell on a later day (not before). Find maximum profit, or 0 if no profit.

What the Interviewer is Evaluating

Do you clarify that sell must come after buy?

Common Mistake

Allowing selling before buying, or selling on the same day.

Input:  prices = [7,1,5,3,6,4]
Output: 5 (buy at 1, sell at 6)
Input:  prices = [7,6,4,3,1]
Output: 0 (prices only fall, no profit)

O — OBSERVE

O

OBSERVE

Question

What's the key insight?

Focus

For each day, you want to sell at the highest price you can reach. That means: track the minimum price seen so far, then compute profit = current price − minimum.

What the Interviewer is Evaluating

Can you recognize the need to track the minimum dynamically?

Common Mistake

Checking all pairs (O(n²)) when a single pass works.

Example: [7, 1, 5, 3, 6, 4]
• Day 0: price=7, min=7, profit=0
• Day 1: price=1, min=1, profit=0
• Day 2: price=5, min=1, profit=4
• Day 3: price=3, min=1, profit=2
• Day 4: price=6, min=1, profit=5 ← max
• Day 5: price=4, min=1, profit=3

P — PSEUDOCODE

P

PSEUDOCODE

Question

What's my plan?

Focus

Track minimum price seen. For each price, compute potential profit. Keep the maximum.

What the Interviewer is Evaluating

Clear logic without code-like syntax.

Common Mistake

Pseudocode that's too vague or already in Python.

1. Initialize min_price = first price
2. Initialize max_profit = 0
3. For each price starting from day 1:
   a. Compute profit = price − min_price
   b. Update max_profit if profit is larger
   c. Update min_price if current price is smaller
4. Return max_profit

I — IMPLEMENT

I

IMPLEMENT

Question

How do I code this cleanly?

Focus

Initialize correctly. Update min_price and max_profit in the right order.

What the Interviewer is Evaluating

Correct variable updates and loop logic.

Common Mistake

Updating min_price before computing profit (or vice versa).

def maxProfit(prices):
    """
    Find the maximum profit from one buy-sell transaction.
    
    Args:
        prices: List of integers representing daily stock prices
    
    Returns:
        Maximum profit possible, or 0 if no profit
    """
    if not prices or len(prices) < 2:
        return 0
    
    min_price = prices[0]
    max_profit = 0
    
    for price in prices[1:]:
        profit = price - min_price
        max_profit = max(max_profit, profit)
        min_price = min(min_price, price)
    
    return max_profit

T — TEST

T

TEST

Question

Does my code work on all cases?

Focus

Test: normal profit, no profit, single element, two elements, all increases/decreases.

What the Interviewer is Evaluating

Edge case handling and correctness.

Common Mistake

Not testing edge cases like short arrays.

Test 1: [7,1,5,3,6,4] → 5 ✓
Test 2: [7,6,4,3,1] → 0 (falling prices) ✓
Test 3: [1] → 0 (single element) ✓
Test 4: [2,4] → 2 (simple profit) ✓
Test 5: [2,1,2,0,1] → 1 ✓
Test 6: [1,2,3,4,5] → 4 (buy at 1, sell at 5) ✓

A — ANALYZE

A

ANALYZE

Question

What's the complexity?

Focus

Single pass through prices. Each operation (comparison, update) is O(1).

What the Interviewer is Evaluating

Correct complexity reasoning.

Common Mistake

Counting nested loops or forgetting that min/max are O(1).

TIME O(n)
SPACE O(1)

Why: We iterate through prices once (O(n)). For each price, we do constant-time comparisons (O(1)). We store only a few variables: min_price, max_profit.

Brute force: Checking all pairs is O(n²). This single-pass approach is much better.