Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Buy Low Sell High DP

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

Your question is Buy Low Sell High DP. 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

Two Sigma's market analytics pipeline receives a chronological list of prices for a security. Given at most k transactions, compute the maximum possible profit by choosing when to buy and sell.

A transaction consists of one buy followed by one sell. You must sell before buying again, cannot hold more than one position at a time, and cannot buy or sell before the corresponding price is available. If no profitable strategy exists, return 0.

Formal Specification

Implement max_profit(prices, k), where prices is a list of integers and k is a nonnegative integer. Return an integer representing the maximum profit. Each transaction's profit is sell_price - buy_price.

Constraints

  • 0 <= k <= 100
  • 0 <= prices.length <= 1,000
  • 0 <= prices[i] <= 10^6
  • A transaction requires one buy followed by one sell
  • At most one stock may be held at a time

Function Signature

def max_profit(prices, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output