Skip to main content

13. Best Time to Buy and Sell Stock

easyAsked at Palantir

Given an array prices where prices[i] is the price of a given stock on day i, find the maximum profit from a single buy/sell. Palantir uses this to test whether you spot the running-min-then-diff streaming pattern they apply to anomaly detection across time-series rows in a Foundry dataset.

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

Source citations

Public interview reports confirming this problem appears in Palantir loops.

  • Blind (2026-Q1)Palantir FDE phone-screen warm-up before a sliding-window follow-up.
  • Glassdoor (2025-10)Reported by 3 candidates in the same hiring window.

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 <= 10^5
  • 0 <= prices[i] <= 10^4

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 5.

Example 2

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

Explanation: Prices fall every day — no profit possible.

Example 3

Input
prices = [2,4,1]
Output
2

Approaches

1. Brute force pairwise

Check every (buy, sell) pair where buy < sell; track the max diff.

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

Tradeoff: Quadratic — TLEs on n=10^5. Palantir will absolutely push you to do better.

2. Single pass running minimum

Track the minimum price seen so far; at each day compute profit = current - minSoFar and update the answer.

Time
O(n)
Space
O(1)
function maxProfit(prices) {
  let minSoFar = Infinity;
  let best = 0;
  for (const price of prices) {
    if (price < minSoFar) minSoFar = price;
    else if (price - minSoFar > best) best = price - minSoFar;
  }
  return best;
}

Tradeoff: Linear time, constant space, single pass. The streaming pattern is the bonus signal.

Palantir-specific tips

Palantir grades this on whether you articulate that the optimal solution is a streaming algorithm — works on infinite tick data, never re-reads. Connect it explicitly: in Foundry, the same shape powers anomaly detection over telemetry rows where you maintain a rolling min/max as new partitions land. Drop the phrase 'this is one-pass O(1) extra state' before coding to lock in the signal.

Common mistakes

  • Initializing minSoFar to prices[0] but iterating from i=0, so the first comparison is always 0.
  • Using Math.min(0, ...) on the profit, capping the answer at 0 incorrectly when prices monotonically rise.
  • Sorting the array — you destroy the temporal constraint that buy must precede sell.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions.
  • With cooldown (LC 309) — DP.
  • With at most k transactions (LC 188) — DP, harder.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the running-min approach work in one pass?

Because the best sell-on-day-i is bounded by the cheapest buy from days 0..i-1. We don't need to know which day we bought — only the minimum so far — so we maintain a single scalar.

What if prices[i] can be negative?

Same logic works — you still want the largest (sell - buy) gap. The constraint here says 0 <= prices[i], but the algorithm doesn't care.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →