Back to questions
You're on Meta (Facebook's) Newsfeed Ads Team, working on an experiment to increase the number of video ads served up in a person's newsfeed.
The newsfeed ranking team gives you a list of which denotes what type of content their algo is planning to serve a user.
You want to insert n more video ads; however, you can't place a video ad next to an existing video otherwise users will be annoyed about Meta's newsfeed being too video-heavy. Similarly, you can't place a video ad next to a non-video ad, because you don't want to overwhelm the user with consecutive ads.
Is it possible to insert more video ads into the feed based on ?
Input: feed_items = [1, 2, 0, 0, 0], n = 3
Output: True
Explanation:
Thus, we can insert 3 out of 3 video ads, so we return True.
Input: feed_items = [1, 0, 2, 0, 1, 1, 0], n = 2
Output: False
Explanation:
The only place where we can place a video ad is at the end of , so we can't insert all 2 video ads as requested, hence we return False.
To determine if we can place ads in a list of posts (), we’ll look for spaces between normal posts (s) where ads can go. By counting these spaces, we can see if placing ads is possible.
Since ads can only go between normal posts:
If our count of valid slots reaches , we can place all required ads and return .
Here's the code:
Another way to find spots for ads is by counting consecutive zeros:
Here's is the code;