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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Stock Trading Profit Maximization
00:00
5 left

Stock Trading Profit Maximization

HardPython

Problem

Given a list of stock amounts and their profits and an initial budget to buy stocks, maximize total profit (solvable with a priority queue).

Asked in the Round 2 - DSA / Problem Solving stage. Candidate realized only afterward that a priority queue was the intended optimal approach; the interviewer was described as non-interactive, making the problem statement hard to clarify.

Implement maximize_profit(k, initial_budget, capital, profits). Each project requires the corresponding capital[i], returns profits[i], and can be selected at most once. Return the final budget after selecting at most k affordable projects. A project is affordable when its capital requirement is at most the current budget, and its profit is added immediately.

Constraints

  • 0 <= k <= len(capital)
  • 1 <= len(capital) = len(profits) <= 10^5
  • 0 <= initial_budget <= 10^9
  • 0 <= capital[i], profits[i] <= 10^9
  • Each project can be selected at most once

Function Signature

def maximize_profit(k, initial_budget, capital, profits):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.