Max Sum Subsequence of Signal Readings With Gaps of at Most K
Last updated: November 29, 2026
Quick Overview
Sensor readings can be positive or negative, and you want a non-empty subsequence maximizing the sum where consecutive chosen indices are at most k apart. Return the maximum sum.
0
0
0 solved
Sensor readings can be positive or negative, and you want a non-empty subsequence maximizing the sum where consecutive chosen indices are at most k apart. Return the maximum sum.
A Google onsite hard where the obvious DP is O(n k) and the expected solution uses a monotonic deque to get the window maximum in O(1). Interviewers look for a clear statement of the DP first and then the data-structure optimization.
What the Interviewer Expects
- Define best[i] as the best valid subsequence ending at i
- Write best[i] = a[i] + max(0, max of best over the last k indices)
- Maintain the window maximum with a decreasing deque
- Pop expired indices from the front
- Handle all-negative inputs
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
- Require the subsequence to start at index 0 and end at index n - 1.
- Gaps must be at least g and at most k.
- Use a heap instead of a deque. What is the complexity?
- Return the chosen indices.
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: list a of integers and integer k >= 1. Choose indices i1 < i2 < ... (at least one) with each i_{t+1} - i_t <= k. Output the max sum. With all negatives, the answer is the largest single ele...
High-Level Design
Let best[i] be the max sum of a valid subsequence ending at i. It either starts at i or extends one ending within the previous k indices: best[i] = a[i] + max(0, max(best[i-k..i-1])). A direct loo...