Longest subsequence with at most k value changes
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Snowflake. It tests dynamic programming, subsequence and arrays.
You get an integer array elements and a non-negative integer k.
Choose a subsequence of elements: delete any number of items (possibly none) and keep the rest in their original order. A change is a pair of neighbours in the chosen subsequence whose values differ. For a subsequence s of length m, the number of changes is the count of i with 0 <= i < m - 1 and s[i] != s[i + 1].
Return the maximum length of a subsequence that has at most k changes.
def max_length(elements: list[int], k: int) -> int: ...
Constraints
0 <= len(elements) <= 5000-10^9 <= elements[i] <= 10^90 <= k <= 50
Example 1
elements = [3, 8, 3, 8, 3], k = 0
output = 3
With no changes allowed, every kept value must be the same. Keeping the three 3s gives length 3. Note that a subsequence may skip items, so the answer is not limited to one contiguous run.
Example 2
elements = [6, 6, 2, 2, 6, 5], k = 1
output = 4
[6, 6, 2, 2] has one change (6 to 2). So does [6, 6, 6, 5]. No subsequence of length 5 has at most one change.
Clarifying questions
- Is the empty subsequence allowed, so an empty array returns 0?
- A change is any differing neighbour pair, so going back to an earlier value counts again (
3, 8, 3has two changes). Is that right? - With
nup to 5000 andkup to 50, is O(n·k) the target, or is O(n²·k) acceptable? - Can
kbe larger than the number of distinct values, or thann - 1?
What a strong answer covers
- Dynamic programming over "longest subsequence ending in value
vwith at mostjchanges". When elementxarrives, extend either the best subsequence already ending inxwith the samej, or the best subsequence ending in any value withj - 1changes. - Keeps the best length for each
jacross all values, so each element costs O(k): O(n·k) time and O(distinct·k) space. - Reads the previous state before updating it for the current element (or walks
jdownwards), so one element is not counted twice. - Explains why greedy ideas such as "keep the
k + 1most frequent values" fail: the order of the values matters. - Edge cases: empty array,
k = 0(the answer is the highest frequency of one value), all values equal,k >= n - 1(the answer isn), large and negative values (use a hash map, not an array index). - Tests against a brute force over all subsequences of small random arrays.
Follow-ups
- Return one longest subsequence, not only its length.
- Each change costs
|a - b|, and the total cost must stay within a budget. - Count the subsequences of maximum length, modulo 10^9 + 7.
- Elements arrive one at a time; report the answer after each one.
Where it's asked
What it tests
dynamic programming · subsequence · arrays
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
