Order Ad Slots So One Advertiser Never Repeats Within K

Last updated: November 29, 2026

Quick Overview

Given the advertisers of the ads to place in a feed of native ad slots, return an ordering in which any two ads from the same advertiser are at least k slots apart, or an empty list if no such ordering exists.

StackAdapt
Coding & Algorithms
Software Engineer
StackAdapt
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given the advertisers of the ads to place in a feed of native ad slots, return an ordering in which any two ads from the same advertiser are at least k slots apart, or an empty list if no such ordering exists.

Native placements in a StackAdapt-style feed look spammy if one brand fills consecutive slots, so publishers enforce separation rules. The onsite looks for a greedy with a cooldown queue, a correct impossibility check, and a proof sketch for why taking the most frequent advertiser first works.

What the Interviewer Expects
  • Uses a max-heap of remaining counts plus a cooldown queue
  • Detects impossibility when the heap is empty but ads are still cooling
  • Justifies the greedy choice of the most frequent available advertiser
  • Handles k <= 1 trivially
  • States O(n log a) complexity for n ads and a advertisers
Key Topics to Cover
Greedy
Heap
Scheduling
Cooldown queue
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
  • Can you compute feasibility without building the order?
  • How would you add a rule that an advertiser may not appear in slot 1?
  • Ads have different expected revenue; how do you maximize revenue subject to separation?
  • How would you do this online as ads arrive in a stream?
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: a list of advertiser ids (repeats allowed) and an integer k. If two equal advertisers sit at positions i < j, require j - i >= k. Output any valid ordering using every ad, or [] if impossible....

High-Level Design

Fill slots left to right. At each slot pick the available advertiser with the most ads remaining, because the advertiser with the largest backlog is the one most at risk of running out of room. After ...


Submit Your Answer
Markdown supported

Related Questions