Greatest three-digit subsequence
I was asked this
No reports yet. Sign in to say where you were asked it.
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) <= 500 <= 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.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
