Asked at 12 companies
12 recent
- Tesla
- HCL
- Accenture
- Zoho
- IBM
- Nvidia
- Amazon
- Bloomberg
- Meta
- Apple
+2 moreShow fewer
- tcs
- Adobe
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Counting with a hash map · A set for “have I seen this?”
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · API rate limits: “100 requests per minute” is a sliding window: count what falls inside the last 60 seconds, drop what slides out, and reject the extra.
ProblemProblem statementYou are given an array prices where prices[i] is the price of a stock on day i. Choose one day to buy and a later day to sell for maximum profit. Return the maximum profit (0 if no profit is possible).ReadHide
Problem Statement
You are given an array prices where prices[i] is the price of a stock on day i. Choose one day to buy and a later day to sell for maximum profit. Return the maximum profit (0 if no profit is possible).
Imported 2026-10-09 from NeetCode auto-sync (reviewed; List import added).
Input / Output
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: buy at 1, sell at 6 → profit 5
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Track the Minimum — one pass: remember the cheapest price seen so far, and the best profit achievable by selling today.
Approach
Brute Force — All Pairs, O(n²)
best = 0
for buy in range(n):
for sell in range(buy+1, n):
best = max(best, prices[sell] - prices[buy])
Optimized — Min Price + Best Profit, O(n)
l = 0 (buy pointer: cheapest day so far)
for r in range(1, n):
if prices[r] > prices[l]: best = max(best, prices[r] - prices[l])
else: l = r
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| All pairs | O(n²) | O(1) | Tries every buy/sell combo |
| Min-track | O(n) | O(1) | Cheapest-so-far + best profit |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n^2) time | O(1) space — try every buy/sell pair."""
def maxProfit(self, prices: List[int]) -> int:
best = 0
for buy in range(len(prices)):
for sell in range(buy + 1, len(prices)):
best = max(best, prices[sell] - prices[buy])
return best
class Solution:
"""O(n) time | O(1) space — track cheapest day, best profit."""
def maxProfit(self, prices: List[int]) -> int:
l, best = 0, 0
for r in range(1, len(prices)):
if prices[r] > prices[l]:
best = max(best, prices[r] - prices[l])
else:
l = r
return bestOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language