Best Feed Highlight Reel With Gaps of at Most K Posts

Last updated: November 29, 2026

Quick Overview

Given engagement deltas for posts in feed order (possibly negative) and an integer k, pick a non-empty subsequence where consecutive picks are at most k positions apart, maximizing the total.

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

0

0

0 solved


Given engagement deltas for posts in feed order (possibly negative) and an integer k, pick a non-empty subsequence where consecutive picks are at most k positions apart, maximizing the total.

A Meta onsite Hard that combines dynamic programming with a monotonic deque, framed as building a highlight reel from a Feed timeline where viewers will not tolerate long gaps. The interviewer expects you to derive the O(nk) DP first, then optimize the window maximum to O(n).

What the Interviewer Expects
  • Define dp[i] as the best total of a valid pick sequence ending at i
  • Write the recurrence dp[i] = x[i] + max(0, max(dp[i-k..i-1]))
  • Use a decreasing deque of indices to get the window maximum in amortized O(1)
  • Evict indices that fall out of the k-window
  • Return the max over dp, handling all-negative input
Key Topics to Cover
Monotonic deque
Dynamic programming
Sliding window maximum
Subsequences
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
  • Reconstruct which posts were picked.
  • What if the gap constraint is on timestamps rather than positions?
  • What if picks must be at least m apart as well as at most k?
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 x of integers (length n up to 10^5) and k >= 1. Choose indices i1 < i2 < ... with each i(t+1) - i(t) <= k. Output: the maximum sum of chosen values. At least one element must be chos...

High-Level Design

Let dp[i] be the best sum of a valid sequence that ends by picking i. Either i starts a new sequence, or it extends a sequence ending in the previous k positions, so `dp[i] = x[i] + max(0, max(dp[j]...


Submit Your Answer
Markdown supported