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
Predict Inventory Needs Function
00:00
5 left

Predict Inventory Needs Function

MediumPython

Problem

Meta Shop needs a lightweight demand forecast for each SKU based on recent daily sales. Implement a function that uses a recency-weighted moving average, then calculates how many units should be replenished to cover the lead time and forecast horizon.

For each SKU, consider at most the last window sales values. Assign weights 1, 2, ..., m to the selected values, where m is the number of values and the most recent day receives weight m. The forecasted daily demand is the weighted average. Required inventory is the ceiling of this forecast multiplied by lead_time + horizon. Return only the additional units needed after subtracting current inventory, never returning a negative value.

Formal Specification

Implement forecast_inventory(sales_history, inventory, window, lead_time, horizon).

  • sales_history is a dictionary mapping SKU strings to arrays of nonnegative integers, ordered from oldest to newest.
  • inventory is a dictionary mapping every SKU in sales_history to its current nonnegative unit count.
  • window, lead_time, and horizon are positive integers.
  • Return a dictionary mapping each SKU to its nonnegative replenishment quantity.
  • Every sales history contains at least one value.

Constraints

  • 1 <= number of SKUs <= 10^4
  • 1 <= len(sales_history[sku]) <= 10^5
  • The total number of recorded sales values is at most 10^6
  • 1 <= window, lead_time, horizon <= 10^6
  • All sales and inventory values are nonnegative integers
  • Every SKU in sales_history exists in inventory

Function Signature

def forecast_inventory(sales_history, inventory, window, lead_time, horizon):
Interviewer

Your question is Predict Inventory Needs 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.