← Questions

Longest subsequence with at most k value changes

0
SnowflakeCoding

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^9
  • 0 <= 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, 3 has two changes). Is that right?
  • With n up to 5000 and k up to 50, is O(n·k) the target, or is O(n²·k) acceptable?
  • Can k be larger than the number of distinct values, or than n - 1?

What a strong answer covers

  • Dynamic programming over "longest subsequence ending in value v with at most j changes". When element x arrives, extend either the best subsequence already ending in x with the same j, or the best subsequence ending in any value with j - 1 changes.
  • Keeps the best length for each j across 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 j downwards), so one element is not counted twice.
  • Explains why greedy ideas such as "keep the k + 1 most 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 is n), 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding