Best 3-day sales window

Python data engineering interview problem. Difficulty: intermediate. Pattern: Sliding Window. About 15 minutes. Free to practice.

A store reports daily sales. Planning wants the 3-day window with the highest total so they can staff the peak.

Sales are recorded per day, and you want to know which block of k days in a row had the best total. Return the largest sum of k consecutive values. If k is 0 or less, or the list is shorter than k, return 0. Write best_window_sum(sales, k). In [1, 4, 2, 10] with k = 2, the windows add up to 5, 6 and 12, so the answer is 12. In [1, 2] with k = 3 there is no full window, so the answer is 0. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Look at every block of k consecutive values.
  • Return the largest sum.
  • Return 0 when no block of size k exists.
  • Return 0 when k is 0 or less.

Constraints

  • sales is a list of numbers.
  • k is a whole number.

Examples

Input: best_window_sum([1, 4, 2, 10], 2) Output: 12 This input follows the stated rules and produces this output.

Topics: lakebench, python, sliding-window.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Best 3-day sales window

Interview-style drill: Given daily sales figures, find the 3-day consecutive window with the highest total.

Find the window of length k with the largest sum of sales.