FastPrepLargest K-Digit Subsequence
Problems/Largest K-Digit Subsequence

Largest K-Digit Subsequence

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report points to the maximize version of a well-known remove-digits problem and calls out a greedy approach with a stack. The input limits and examples were not kept. Our version is about 96% the same as the reported question.

You are given a decimal digit string digits and an integer k. Select exactly k digits while preserving their relative order.

Return the lexicographically largest length-k digit string that can be formed. Leading zeroes are retained.

Function

largestKDigitSubsequence(digits: String, k: int) → String

Examples

Example 1

digits = "1924"k = 2return = "94"

Choosing 9 and 4 gives the largest two-digit subsequence.

Example 2

digits = "4177252841"k = 4return = "7841"

The selected digits 7, 8, 4, and 1 occur at increasing indices and form the maximum possible four-digit string.

Example 3

digits = "0012"k = 3return = "012"

The first zero can be skipped, but the leading zero in the selected subsequence is retained.

Constraints

  • 1 <= digits.length <= 200000.
  • 1 <= k <= digits.length.
  • digits contains only characters from '0' through '9'.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Remove K DigitsThe D.E. Shaw Group · ONSITE INTERVIEW
  • Remove K DigitsAmazon · ONSITE INTERVIEW
  • Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEW
  • Minimum Remove to Make Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
  • Remove K DigitsMorgan Stanley · ONSITE INTERVIEW
  • Remove K DigitsProcol · PHONE SCREEN
  • Substring RemovalJP Morgan Chase · OA
  • Modify the StringYahoo · OA
  • Lexicographically Minimum Stack EncryptionRippling · OA
  • Shortest String After Erasing Equal PairsMarshall Wace · OA
See Google hiring insights
Saved
class Solution {
    public String largestKDigitSubsequence(String digits, int k) {
        // Write your code here.
    }
}
Checking account…