← Questions

Greatest three-digit subsequence

0
GoogleCoding

You are given a list digits of single digits (each 0–9). Choose at most three of them, keep them in their original order, and read them left to right as a number. Return the largest number you can make.

Leading zeros are allowed: picking 0, 0, 4 gives 4.

Implement:

def greatest_triple(digits: list[int]) -> int: ...

Constraints

  • 3 <= len(digits) <= 50
  • 0 <= digits[i] <= 9

Example 1

digits = [2, 8, 1, 7, 3, 6]
output = 876

8 (index 1), 7 (index 3), 6 (index 5). No pick can start with a larger digit, and after the 8 the best two in order are 7 and 6.

Example 2

digits = [4, 6, 9]
output = 469

The 9 is last, so it cannot be the first digit of a three-digit pick, and any shorter pick is smaller.

Be ready to explain why choosing fewer than three digits never helps, and how your method changes if you must pick exactly k digits.

Reconstructed from the reported question; details may differ from what interviewers ask.

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