Reported April 2024
Coherestring

Remove One Five for the Largest Number

Reported by candidates from Cohere's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Cohere OA. Under 2s to a working solution.
Founder's read

The usual way to fail Cohere's "Remove One Five" question, reported in April 2024, is to treat every input like a positive number. It looks like a trivial string task. Remove exactly one digit 5, return the largest integer. But the sign flips the whole strategy, and the edge cases around leading zeros and empty results are where first attempts die. With a short input length, brute force passes, but only if you format the output right. If you freeze on the negative branch, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.

The problem

Given the canonical decimal representation of an integer as a string number, remove exactly one occurrence of the digit 5 and return the canonical decimal representation of the largest integer obtainable.
The input may begin with -. Strip leading zeros from the result, and return 0 when every remaining digit is zero or no digit remains.

Function
removeOneFive(number: String) → String

Examples
Example 1
number = "5456"
return = "546"
Removing the first 5 gives 456; removing the second gives 546, which is larger.
Example 2
number = "-5155"
return = "-155"
Among negative candidates, the one with the smallest magnitude is largest.

Constraints
1 <= number.length <= 17.
The input is a valid canonical decimal integer and contains at least one digit 5.
The result fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The cleanest approach is to try every position holding a 5, build the string without it, normalize it, and compare. With at most 17 characters, that's trivial. The greedy version works too. For positives, remove the first 5 that sits before a larger digit, otherwise the last 5. For negatives, you want the smallest magnitude, so remove the first 5 that sits before a smaller digit, otherwise the last 5. The pitfall is normalization. Removing a 5 can leave leading zeros, like "50" becoming "0" or "-505" becoming "-05". An empty string must become "0", and "-0" must become "0". Don't compare candidates as raw strings, since lengths and signs break that. Parse them as integers, which fits in 64 bits. If you blank on the sign logic mid-assessment, StealthCoder is the hedge that hands you the enumerate-and-compare version.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Remove One Five for the Largest Number cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder
⏵ The honest play

You've seen the question. Make sure you actually pass Cohere's OA.

Cohere reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Remove One Five for the Largest Number FAQ

How hard is the Cohere Remove One Five problem really?+

Easy on algorithm, tricky on details. The input is at most 17 characters, so brute force over each 5 is fine. Most lost points come from negative numbers, leading zeros, and empty or "-0" results, not from the core idea.

What's the trick to getting the largest result?+

Enumerate every index holding a 5, delete it, normalize the string, and keep the max. That sidesteps the greedy sign reasoning entirely. Normalizing means stripping leading zeros, mapping empty or all-zero to "0", and never returning "-0".

How should I handle negative inputs?+

Flip your goal. A negative number is largest when its magnitude is smallest. So you want the shortest or smallest digit string after removal. Parsing each candidate as a signed integer and taking the max handles this without special cases.

Which edge cases should I test before submitting?+

Test "5" which should return "0", "-5" which should return "0", "50", "-505", and "5555". Also test a number where the 5 is first and followed by zeros. These cover empty results, leading zeros, and negative zero.

How do I prepare for this in 48 hours?+

Practice string slicing, integer parsing, and writing a small normalize helper. Solve this one both ways, brute force and greedy, and compare outputs on random inputs. Since the April 2024 report suggests a short coding question, speed on edge cases matters more than fancy algorithms.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Cohere.

OA at Cohere?
Invisible during screen share
Get it