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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Heap
Offline queries
Sorting
Sweep line
Lazy deletion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported