Delete One '5' From a Digit String to Get the Largest Number
Company: Cohere
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Given a digit string containing at least one 5, delete exactly one 5 so the remaining string has the largest possible numeric value, keeping any leading zeros. This intern online-assessment problem tests reasoning about how each digit position affects a number and careful handling of repeated digits.
Constraints
- 2 <= len(digits) <= 100000
- Every character of digits is one of '0' through '9'.
- digits[0] != '0'
- digits contains at least one '5'.
Examples
Input: ("15958",)
Expected Output: "1958"
Input: ("5505",)
Expected Output: "550"
Hints
- Compare deleting the '5' at index i with deleting a later '5' at index j: the two results agree before i. Where do they first differ?
- Removing a '5' pulls its right neighbour one position to the left. When does that make the number bigger at the earliest possible position?
- If no '5' ever benefits from being removed in that way, which '5' costs the least to remove?