Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Maximize Profit from Stock Prices

MediumPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Maximize Profit from Stock Prices. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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