Shortest Ad Campaign Active at Each Query Timestamp
Last updated: November 29, 2026
Quick Overview
Each ad campaign runs over an inclusive day range [start, end]. For each query day, return the length (end - start + 1) of the shortest campaign that is active on that day, or -1 if none is.
Meta
November 29, 20260
0
0 solved
Each ad campaign runs over an inclusive day range [start, end]. For each query day, return the length (end - start + 1) of the shortest campaign that is active on that day, or -1 if none is.
This offline query problem is in the style of Meta's harder onsite rounds, framed around the ads delivery system choosing the most targeted (shortest) active flight for a given day. The interviewer wants sorting both campaigns and queries, then a min-heap keyed by length with lazy removal of expired campaigns.
What the Interviewer Expects
- Recognizes per-query scanning is O(n * q)
- Sorts queries while remembering their original indices
- Pushes campaigns into a heap as their start passes the query day
- Lazily discards campaigns whose end is before the query day
- Explains why lazy deletion is correct for increasing query days
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 arbitrary order?
- How would you return the campaign ID rather than the length?
- What changes if you want the longest active campaign?
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: campaigns as inclusive [start, end] and queries as days. Output: list of shortest active lengths in the original query order, -1 where none apply. n, q up to 10^5....
High-Level Design
Process queries in increasing day order. Keep campaigns sorted by start and a pointer into them. For each query day d, push every campaign with start <= d onto a min-heap as (length, end). Then pop ...