Largest Palindromic Number from Available Digits
Updated
Problem statement
String S contains decimal digits. Use one or more of its digits to form a palindromic number with the greatest possible decimal value.
- You may reorder the chosen digits and leave other digits unused.
- You may not use a digit more times than it appears in
S. - The answer has no leading zero, except that the one-digit number
0is allowed.
Return the answer as a string. A palindrome reads the same from left to right and right to left.
Function
solution(S: String) → StringExamples
Example 1
S = "8199"return = "989"Among the possible values 1, 8, 9, 99, 919 and 989, the greatest is 989.
Example 2
S = "39878"return = "898"Use the two eights as a pair and nine as the centre.
Example 3
S = "00900"return = "9"Zero pairs cannot become leading zeros, so the greatest valid result is the single nine.
Example 4
S = "000"return = "0"Only the one-digit zero avoids a leading zero.
Example 5
S = "54321"return = "5"No pair exists, so choose the largest available single digit.
Constraints
1 <= S.length <= 100000.Scontains only digits from0through9.
More Marshall Wace Online Assessment questions
- Count Letters with Ordered Lowercase and Uppercase OccurrencesOA · Seen Oct 2025
- Maximum Trailing Zeros on a Path with at Most One TurnOA · Seen Oct 2025
- Shortest Subarray Covering an Integer RangeOA · Seen Oct 2025
- Maximum Events in a 60-Second WindowOA · Seen Aug 2025
- Successful Logins from Multiple CountriesOA · Seen Aug 2025
- Maximize Remaining Item Types After Clearing Consecutive ShelvesOA · Seen Nov 2024
- Shortest String After Erasing Equal PairsOA · Seen Nov 2024
- Count Distinct Tags in a Fixed-Radius Circle (Practice Reconstruction)OA · Seen Apr 2024
- Generate Unique Company Email AddressesOA · Seen Apr 2024
- Reconstruct a Two-Row Binary MatrixOA · Seen Apr 2024
Similar questions at other companies
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Viral Content BalancerTikTok · OATikTok interview questions
- Find Minimum Time RequiredAmazon · OA
- Merging PalindromesOld Mission · OA
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Minimum Substrings Without Repeating CharactersMicrosoft · OAMicrosoft interview questions
- Find Length of Longest Good SubsequenceWalmart · OAWalmart interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEWAnthropic interview questions