Insert Sponsored Posts Into a Feed Without Adjacent Ads

Last updated: November 29, 2026

Quick Overview

A feed is a list of 0s (organic slots that can take an ad) and 1s (existing ads). Determine whether n new sponsored posts can be placed into 0 slots so that no two ads are adjacent, and return the maximum number that could be placed.

LinkedIn
Coding & Algorithms
Software Engineer
LinkedIn
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A feed is a list of 0s (organic slots that can take an ad) and 1s (existing ads). Determine whether n new sponsored posts can be placed into 0 slots so that no two ads are adjacent, and return the maximum number that could be placed.

A product-flavored version of a classic LinkedIn greedy warm-up. The interviewer wants a one-pass greedy, a short argument for why taking the leftmost legal slot is optimal, and clean boundary handling.

What the Interviewer Expects
  • Scans left to right placing an ad whenever left and right neighbors are free
  • Treats out-of-bounds neighbors as free
  • Argues greedy optimality with an exchange argument
  • Can stop early once n ads are placed
  • Does not mutate the caller's list, or says so
Key Topics to Cover
greedy
arrays
exchange argument
constraints
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 ads must be at least d slots apart?
  • What if some slots have higher value and you want to maximize total value?
  • How would you handle a feed that grows as the member scrolls?
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: feed list of 0 and 1 that already has no adjacent 1s, and n >= 0. Output: (can_place, max_placeable). A slot i is usable if feed[i] == 0 and both neighbors (if they exist) are 0 after p...

High-Level Design

Greedy: walk left to right, and whenever slot i is free with free neighbors, place an ad there. Exchange argument: take any optimal placement and its leftmost ad. Moving it to the greedy's leftmost le...


Submit Your Answer
Markdown supported

Related Questions