Smallest Promotion Window Containing Each Order Timestamp
Last updated: November 29, 2026
Quick Overview
Given promotion windows as inclusive [start, end] minute ranges and a list of order timestamps, return for each order the length (end - start + 1) of the shortest promotion window containing it, or -1 if none does.
Amazon
November 29, 20260
0
0 solved
Given promotion windows as inclusive [start, end] minute ranges and a list of order timestamps, return for each order the length (end - start + 1) of the shortest promotion window containing it, or -1 if none does.
An Amazon onsite hard question about attributing each order to the most specific active promotion. The interviewer wants offline query processing: sort queries and windows, sweep time, and keep a heap of active windows keyed by length with lazy removal of expired ones.
What the Interviewer Expects
- Recognize per-query scanning of all windows is O(n * q)
- Sort windows by start and queries by time while remembering query order
- Add windows that have started into a min-heap keyed by length
- Pop windows whose end is before the query
- Map answers back to original query order
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if queries must be answered online in arrival order?
- Return the promotion ID rather than its length, breaking ties by earliest start.
- How would you handle windows being added and removed over time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: windows list of [start, end] with start <= end, and orders list of integer timestamps. Output: list where entry i is the shortest containing window length for orders[i], else -1....
High-Level Design
Process queries in increasing time. Before answering a query at time t, push every window with start <= t into a min-heap as (length, end). Then pop from the top while the top's end < t: those w...