Problem · String

Maximum Number Possible

EasyMicrosoftFULLTIMEOA
See Microsoft hiring insights

Given a String S representing positive digit containing n numbers, and given k ( number of moves), you can replace any number other then 5, return the maximun number possible within k moves

Examples
01 · Example 1
s = "1839559"
k = 4
return = "5855555"
This test case was added on 05-24-2025. You can find the relevant source ss in the Problem Source section below. Happy coding!
02 · Example 2
s = "5567855"
k = 4
return = "IMPOSSIBLE"
Becasue there are 3 digits in S that are not 5, so we cant do the 4th operation. This test case was added on 05-24-2025. You can find the relevant source ss in the Problem Source section below. Coding is fun!
03 · Example 3
s = "165232"
k = 3
return = "555552"
Becasue there are 3 digits in S that are not 5, so we cant do the 4th operation. This test case was added on 05-24-2025. You can find the relevant source ss in the Problem Source section below. Coding is fun! It comes from Source 2, but the expected output there seems to conflict with what’s shown in Source 1. Source 1 states the expected output is "565552".
04 · Example 4
s = "165232"
k = 3
return = "565552"
This test case comes from Source 1, but the expected output there seems to conflict with what’s shown in Source 2. Source 2 states the expected output is 555552.
Constraints
  • An integer N in the range [1..100,000] — the length of a string S
  • An integer K in the range [0..100,000]
  • A string S of length N consisting only of digits (0-9), with no leading zeros.
  • Full constraints updated on 05-24-2025 :)
  • More Microsoft problems
    drafts saved locally
    public String maximumNumberPossible(String s, int k) {
      // write your code here
    }
    
    s"1839559"
    k4
    expected"5855555"
    sign in to submit