Validate Ada Integer Literal
Updated
Problem statement
In the Ada programming language, an integer can be written in two forms. Given a string s, return true if s is a valid integer in one of these forms, and false otherwise.
Underscores _ are only separators: they may appear anywhere in s, any number of times, and are ignored. So first remove every underscore. The string that is left must have one of these two forms:
- Decimal form: one or more decimal digits
0to9, such as2024. - Based form:
base#digits#. Thebasepart is one or more decimal digits0to9, and its value must be from2to16. Thedigitspart is one or more digits of that base. The characters0to9stand for the values 0 to 9, andatoforAtoFstand for 10 to 15. The value of each digit must be less than the base. Both#signs are required, and nothing may come after the second one.
Leading zeros are allowed in both parts, so 007 and 016#ff# are valid. Nothing else is allowed: there is no sign, decimal point or exponent, so strings such as -5, 1.5 and 1e6 are not valid.
Function
isValidAdaInteger(s: String) → booleanExamples
Example 1
s = "123_456_789"return = trueRemoving the underscores leaves 123456789. It has no # and every character is a decimal digit, so it is a valid number in decimal form and the answer is true.
Example 2
s = "16#123abc#"return = trueThis is the based form with base 16. Between the two # signs, 1, 2 and 3 have the values 1 to 3, and a, b and c have the values 10, 11 and 12. Every value is less than 16, so the answer is true.
Example 3
s = "10#123abc#"return = falseThe base is 10, so the value of each digit must be less than 10. 1, 2 and 3 pass, but a, b and c have the values 10, 11 and 12, which are not less than 10. So the answer is false.
Example 4
s = "8#1_7_8#"return = falseRemoving the underscores leaves 8#178#, so the base is 8 and each digit must be at most 7. 1 and 7 pass, but the last digit 8 equals the base, so the answer is false.
Constraints
1 <= s.length <= 100sconsists of English letters, digits and the characters'_','#','+','-'and'.'.
More eBay Onsite Interview questions
- Extract Hashtags from a TweetONSITE INTERVIEW · Seen Nov 2026
- Count Distinct Pairs With Sum KONSITE INTERVIEW · Seen Nov 2026
- Diameter of Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Count Staircases with Strictly Increasing StepsONSITE INTERVIEW · Seen Oct 2026
- Hierarchical Permission CheckONSITE INTERVIEW · Seen Oct 2026
- Matrix MultiplicationONSITE INTERVIEW · Seen Oct 2026
- N-Queens Solution CountONSITE INTERVIEW · Seen Oct 2026
- Gas StationONSITE INTERVIEW · Seen Oct 2026
- Onion-Peel Layers of a Binary TreeONSITE INTERVIEW · Seen Oct 2026
- Container With Most WaterONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Non-Repeating CharacterAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Container Ledger with TradesFlexport · ONSITE INTERVIEWFlexport interview questions
- Christmas Tree with N TiersVerily · ONSITE INTERVIEW
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Implement tail -nConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions