Skip to main content

15. Best Time to Buy and Sell Stock

easyAsked at PayPal

Find the maximum profit from a single buy-sell transaction in a price array. PayPal treats this as a core financial-reasoning problem — interviewers look for candidates who can frame it as tracking running minimum and maximum gain.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in PayPal loops.

  • Glassdoor (2026)PayPal SWE L3 candidate reported stock problem as first coding question
  • Blind (2025)Multiple PayPal reports cite stock buy/sell in phone screens and OAs

Problem

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Constraints

  • 1 <= prices.length <= 100000
  • 0 <= prices[i] <= 10000

Examples

Example 1

Input
prices = [7,1,5,3,6,4]
Output
5

Explanation: Buy on day 2 (price=1), sell on day 5 (price=6), profit = 6-1 = 5

Example 2

Input
prices = [7,6,4,3,1]
Output
0

Explanation: No transaction yields a profit; return 0

Approaches

1. Brute force (all pairs)

Try every buy-sell pair (i, j) where i < j and track the maximum difference.

Time
O(n^2)
Space
O(1)
function maxProfit(prices) {
  let max = 0;
  for (let i = 0; i < prices.length; i++) {
    for (let j = i + 1; j < prices.length; j++) {
      max = Math.max(max, prices[j] - prices[i]);
    }
  }
  return max;
}

Tradeoff: O(n^2) — too slow for 100k prices; mention it as the naive baseline before optimizing.

2. Single-pass minimum tracker

Track the lowest price seen so far and update the maximum profit on every step. Profit at each day = current price minus the best buy price seen before it. One loop, constant space.

Time
O(n)
Space
O(1)
function maxProfit(prices) {
  let minPrice = Infinity;
  let maxProfit = 0;
  for (const price of prices) {
    if (price < minPrice) {
      minPrice = price;      // found a cheaper buy day
    } else if (price - minPrice > maxProfit) {
      maxProfit = price - minPrice; // new best profit
    }
  }
  return maxProfit;
}

Tradeoff: Kadane's-style insight: we never need to look back once we track the running minimum. At PayPal, frame this as 'optimal trade execution with a single market position'.

PayPal-specific tips

PayPal interviews focus on payment processing, fraud detection logic, financial reconciliation algorithms, and distributed transaction design. Hash maps, sliding windows, and two-pointer techniques appear frequently.

Common mistakes

  • Initializing minPrice to 0 instead of Infinity, making day-0 high prices never candidates for minimum
  • Returning a negative profit instead of 0 when prices only decrease
  • Selling before buying — not enforcing j > i in the brute force or assuming buy and sell on same day is valid

Follow-up questions

An interviewer at PayPal may pivot to one of these next:

  • Allow multiple transactions (buy/sell as many times as you want) — LC 122
  • Allow at most two transactions — LC 123
  • Add a cooldown of one day between sell and next buy — LC 309

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Can you buy and sell on the same day?

No — the problem says you must sell on a DIFFERENT future day, so profit would be zero anyway. Clarify this with the interviewer if not stated.

Why does PayPal ask financial-themed algorithm problems?

PayPal evaluates whether candidates can reason about time-ordered financial data, which mirrors real ledger and pricing-engine work. Framing your solution in financial terms signals domain awareness.

Practice these live with InterviewChamp.AI

Drill Best Time to Buy and Sell Stock and other PayPal interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →