Dsa
Sliding Window
Easy
Maximum Order Quantity
You are managing a warehouse that handles orders for various products. Each order has a date and a quantity. You want to find the maximum total quantity of orders that can be fulfilled within any given consecutive 'k' days.
Write a function that takes a list of orders (represented as tuples of (date, quantity)) and an integer k, and returns the maximum total quantity for any consecutive k-day period.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 10^5), the number of orders.
- The next n lines each contain a string date in the format 'YYYY-MM-DD' and an integer quantity (0 ≤ quantity ≤ 1000) separated by a space.
- The last line contains an integer k (1 ≤ k ≤ n).
Output Format:
- Output a single integer, the maximum total quantity that can be fulfilled within any k-day period.
Example:
Input:
5
2023-01-01 10
2023-01-02 20
2023-01-03 30
2023-01-04 10
2023-01-05 50
3
Output:
60
In this example, the maximum total quantity over any 3 consecutive days is from the period 2023-01-03 to 2023-01-05 (30 + 10 + 50 = 90).
Key concepts
sliding_windowarrayalgorithm
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now