Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Predict Inventory Needs Function

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

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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output