Magicsheet logo

Smallest K-Length Subsequence With Occurrences of a Letter

Hard
100%
Updated 6/1/2025

Smallest K-Length Subsequence With Occurrences of a Letter

What is this problem about?

The "Smallest K-Length Subsequence With Occurrences of a Letter" interview question is a high-level string manipulation challenge. You are given a string s, an integer k, a specific letter, and a minimum frequency repetition. You need to find the lexicographically smallest subsequence of s that has length exactly k and contains the letter at least repetition times. This "Smallest K-Length Subsequence coding problem" is complex because it requires balancing lexicographical order with two strict constraints.

Why is this asked in interviews?

This "HARD" level question is asked by companies like Deutsche Bank to test a candidate's mastery of monotonic stacks and greedy strategies. It evaluates your ability to make "locally optimal" choices (picking the smallest character) while ensuring that "globally mandatory" requirements (length k and repetition count) can still be met by the remaining characters in the string.

Algorithmic pattern used

The primary pattern is the "Monotonic Stack and Greedy interview pattern".

  1. Maintain a stack that is as lexicographically small as possible.
  2. As you iterate through the string, try to pop the top of the stack if the current character is smaller, provided that:
    • The remaining characters in s are enough to fill the stack to length k.
    • If the character being popped is the target letter, there must still be enough occurrences of that letter remaining in s to meet the repetition count.
  3. After the loop, the stack might still be longer than k or have too few letters. You must carefully trim it while maintaining the constraints.

Example explanation

Let s = "leet", k = 3, letter = 'e', repetition = 1.

  1. 'l': Stack ['l'].
  2. 'e': 'e' < 'l'. Remaining length from 'eet' is 3 (enough for k=3). 'l' is not 'e'. Pop 'l', push 'e'. Stack ['e'].
  3. 'e': Stack ['e', 'e'].
  4. 't': Stack ['e', 'e', 't']. Result: "eet". If the string was "leeet" and we needed k=3, we would keep the 'e's to be lexicographically smallest.

Common mistakes candidates make

The most frequent mistake is not correctly tracking the "future" count of the required letter. If you pop a required letter, you must be 100% sure you can pick it up again later. Another error is failing to handle the case where the stack needs to be filled with the target letter at the very end to satisfy the repetition requirement, even if those letters are lexicographically larger than other options.

Interview preparation tip

Monotonic stacks are the go-to data structure for "lexicographically smallest subsequence" problems. Always count the frequencies of characters beforehand so you know what's available in the "future" of your iteration. This "Smallest K-Length Subsequence interview question" is one of the most comprehensive tests of this pattern.

Similar Questions