Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Max Profit From Trading Signals
00:00
5 left

Max Profit From Trading Signals

HardPython

Problem

Given two integer arrays prices and strategy, where -1 indicates buying and 1 indicates selling, calculate the maximum possible profit.

A sell closes the most recent unmatched buy. You may complete at most two trades, and you may skip unprofitable trades. Return the maximum total profit.

Contract

prices and strategy are lists of equal length. Return an integer. If a sell has no unmatched buy, ignore it. If multiple buys occur before a sell, the sell uses the most recent buy.

Constraints

  • 1 <= len(prices) = len(strategy) <= 10^5
  • 0 <= prices[i] <= 10^9
  • strategy[i] is either -1 or 1
  • At most two completed trades contribute to the result

Function Signature

def max_profit(prices, strategy):
Interviewer

Your question is Max Profit From Trading Signals. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.