Fewest Edge Posts to Trim So Removed Engagement Equals X
Last updated: November 29, 2026
Quick Overview
A feed shows a row of posts with positive engagement scores. In each step you remove either the leftmost or the rightmost post. Return the minimum number of removals so the removed scores sum to exactly x, or -1 if impossible.
Meta
November 29, 20260
0
0 solved
A feed shows a row of posts with positive engagement scores. In each step you remove either the leftmost or the rightmost post. Return the minimum number of removals so the removed scores sum to exactly x, or -1 if impossible.
A Meta onsite question that rewards reframing. Removing from both ends is awkward, but keeping a contiguous middle block is a plain sliding window, and the interviewer is looking for that flip.
What the Interviewer Expects
- Reframes the task as finding the longest middle subarray with sum total - x
- Uses a positive-value sliding window for that subarray
- Handles target 0 (remove everything) and negative targets (impossible)
- Converts the longest window length into the removal count
- Gives O(n) time and O(1) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Scores can be negative. How does the solution change?
- Return which posts to remove, not just the count.
- Removals from the left cost twice as much as from the right; minimize cost instead.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: scores, a list of positive integers, and x >= 1. Output: the minimum number of end removals whose removed values total exactly x, or -1....
High-Level Design
Whatever survives is one contiguous block in the middle, and its sum must be total - x. Fewest removals means the longest such block. If the target is negative, return -1; if it is 0, everything mus...