Maximize Engagement From Non-Adjacent Feed Ad Slots

Last updated: November 29, 2026

Quick Overview

Each position in a user's upcoming feed has a predicted ad engagement value. Policy forbids ads in two consecutive positions. Return the maximum total engagement you can get by choosing ad positions.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Each position in a user's upcoming feed has a predicted ad engagement value. Policy forbids ads in two consecutive positions. Return the maximum total engagement you can get by choosing ad positions.

TikTok balances ad load against user experience in the For You feed, and spacing rules are a real constraint. This shows up as an easy dynamic programming question in online assessments, where the grader checks for a clean recurrence and O(1) space.

What the Interviewer Expects
  • States the recurrence best[i] = max(best[i-1], best[i-2] + value[i])
  • Explains the take or skip choice at each position
  • Reduces space to two variables
  • Handles empty, single-element and all-zero inputs
  • Mentions how to recover the chosen positions
Key Topics to Cover
1D dynamic programming
take or skip
space optimization
greedy pitfalls
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 k positions apart instead of 2?
  • How would you return the chosen positions as well?
  • What if the feed is circular, so the first and last slots are adjacent?
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: list of non-negative floats or ints values.
  • Output: maximum sum of a subset with no two adjacent indices.
  • Empty feed returns 0....
High-Level Design

Greedy (pick the biggest value, remove neighbors, repeat) fails, for example [3, 4, 3]: greedy picks 4, but 3 + 3 = 6 is better.

DP: let best[i] be the best total using positions 0 to i. At posit...


Submit Your Answer
Markdown supported