Remove Trailing Zeros
Updated
Problem statement
The string num holds the decimal digits of a positive integer. Remove its trailing zeros, which are the 0 digits after its last nonzero digit, and return the digits that remain, also as a string.
A 0 that has a nonzero digit somewhere to its right is not trailing, so it stays. If num does not end in 0, return it unchanged.
Function
removeTrailingZeros(num: String) → StringExamples
Example 1
num = "4070500"return = "40705"The number ends in two zeros, and both go. The zeros between 4 and 7 and between 7 and 5 are inside the number, so they stay.
Example 2
num = "86"return = "86"The last digit is 6, so there is nothing to remove and the number comes back as it was.
Example 3
num = "9000"return = "9"Every digit after the 9 is a zero at the end, so only the 9 is left.
Constraints
1 <= num.length <= 1000numis made of the digits0to9onlynumdoes not start with0
More Autodesk interview questions
- Non-Repeating CharacterONSITE INTERVIEW · Seen Nov 2026
- Reverse Linked ListONSITE INTERVIEW · Seen Nov 2026
- LRU Cache with TTLONSITE INTERVIEW · Seen Oct 2026
- Deep Merge with a Conflict ResolverONSITE INTERVIEW · Seen Mar 2026
- Diagonal Bounce TraversalOA · Seen Oct 2026
- Interleave Array HalvesOA · Seen Oct 2026
- Latest Arrival to Catch a BusOA · Seen Oct 2026
- Find Top Two TeamsOA · Seen Sep 2026
- In-Memory Database with TTLOA · Seen Mar 2026
Similar questions at other companies
- Extract Hashtags from a TweeteBay · ONSITE INTERVIEWeBay 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