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
Optimizing Battery Usage Function
00:00
5 left

Optimizing Battery Usage Function

MediumPython

Problem

Can you write a function to optimize battery usage based on user data for American Battery Technology?

Treat each action as (action_id, battery_cost, utility). Repeatedly select the remaining action with the highest utility-to-cost ratio that fits within the remaining battery budget. Zero-cost actions with positive utility have the highest priority. Break equal-ratio ties by original input order, and return selected action IDs in selection order.

The function must return an empty list when no action fits.

Constraints

  • 0 <= len(user_data) <= 1000
  • 0 <= battery_budget <= 10^9
  • Each action is [action_id, battery_cost, utility]
  • action_id is a non-empty string
  • 0 <= battery_cost <= 10^9
  • 0 <= utility <= 10^9
  • Action identifiers are unique
  • Actions are selected at most once

Function Signature

def optimize_battery_usage(user_data, battery_budget):
Interviewer

Your question is Optimizing Battery Usage Function. 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.