Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Maximize Profit from Stock Prices

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Maximize Profit from Stock Prices. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an array of integers prices where prices[i] is the price of a given stock on the i-th day, return the maximum profit you can achieve from this transaction. You may complete at most one transaction (i.e., buy one and sell one share of the stock). If you cannot achieve any profit, return 0.

Constraints

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

Function Signature

def max_profit(prices: list[int]) -> int:
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output