FastPrepMaximum Number Possible
Problems/Maximum Number Possible

Maximum Number Possible

Microsoft logoMicrosoft● EasyFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a string s representing a positive integer and an integer k. In one operation, choose a digit that is not 5 and replace it with 5.

Perform exactly k operations and return the largest possible resulting number as a string. If s contains fewer than k digits different from 5, return IMPOSSIBLE.

Function

maximumNumberPossible(s: String, k: int) → String

Examples

Example 1

s = "1839559"k = 4return = "5855555"

There are five replaceable digits. To perform exactly four operations while maximizing the result, keep the early digit 8 and replace 1, 3, and the two 9 digits.

Example 2

s = "5567855"k = 4return = "IMPOSSIBLE"

Only three digits are different from 5, so four operations cannot be performed.

Example 3

s = "165232"k = 3return = "565552"

Replace the early digits 1, 2, and 3 with 5. Keeping the digit 6 produces the largest possible result, 565552.

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 interview questions

  • Maximum Score with Prime JumpsOA · Seen Dec 2026
  • Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Authentication SystemOA · Seen Nov 2026
  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Maximum String OperationsOA · Seen Nov 2026
  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
  • Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
  • Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
  • Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
  • Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
  • Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
  • Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Missing WordsVanguard · OAVanguard interview questions
  • Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
  • Smallest String After at Most One DeletionLSEG · OALSEG interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public String maximumNumberPossible(String s, int k) {
      // write your code here
    }

}
Checking account…