FastPrepReplace Characters With the Nearest Prime Code
Problems/Replace Characters With the Nearest Prime Code

Replace Characters With the Nearest Prime Code

Nutanix logoNutanix● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An intern applicant's report, posted in January 2020, covers a Nutanix online assessment that was also given to new-grad applicants. It had two questions in 90 minutes. Its first question is a one-line string task about prime ASCII codes and the nearest prime, with no further detail.

The report does not say which characters can appear or what happens when two primes are equally close. It gives no limits or examples. Here the string holds only capital letters, so every answer is also a capital letter. A tie goes to the smaller prime. The string has 1 to 10^4 letters. The examples are ours.

Our version is about 85% the same as the reported question.

Every character has an ASCII code. For example, A is 65, B is 66 and Z is 90. A prime number is a whole number greater than 1 whose only divisors are 1 and itself.

You are given a string s. Build a new string of the same length, one character at a time:

  • If the character's code is prime, keep the character.
  • Otherwise, replace it with the character whose code is the prime number closest to its code. If a prime below and a prime above are equally close, use the smaller one.

Return the new string.

Function

replaceWithNearestPrime(s: String) → String

Examples

Example 1

s = "HELLO"return = "GCIIO"

H is 72. The primes 71 and 73 are both 1 away, so the smaller one wins: 71 is G. E is 69. The primes 67 and 71 are both 2 away, so it becomes 67, which is C. L is 76. The primes 73 and 79 are both 3 away, so each L becomes 73, which is I. O is 79, a prime, so it stays. The answer is "GCIIO".

Example 2

s = "NUTANIX"return = "OSSCOIY"

N (78) becomes 79, O. U (85) is 2 away from 83 and 4 away from 89, so it becomes 83, S. T (84) becomes 83, S. A (65) is 2 away from 67 and 4 away from 61, so it becomes 67, C. I (73) is prime and stays. X (88) becomes 89, Y. The answer is "OSSCOIY".

Example 3

s = "CGIOSY"return = "CGIOSY"

The codes are 67, 71, 73, 79, 83 and 89. All of them are prime, so nothing changes.

Constraints

  • 1 <= s.length <= 10^4
  • s consists only of capital English letters (A to Z).

More Nutanix Online Assessment questions

  • Good SubsequencesOA · Seen Jul 2026
  • Min Cost to Connect All PointsOA · Seen Jun 2026
  • Digit Changes for a Multiple of 9OA · Seen Aug 2025
  • Gas Station Loop StartOA · Seen Aug 2025
  • Number of Players (for MTS2)OA · Seen Oct 2024
  • Work Schedules (for MTS2)OA · Seen Oct 2024
  • Minimum Operations to Convert ArrayOA · Seen Apr 2024
  • No Adjacent CharactersOA · Seen Apr 2024
  • Build a BST From a ListOA · Seen Nov 2022
  • Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022

Similar questions at other companies

  • Identification Number Check DigitAbridge · OAAbridge interview questions
  • Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions
  • Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
  • Is This Date a Weekend?Salesforce · OASalesforce interview questions
  • Triple-Position Alphabet EncodingArm · OA
  • Closest ColorTwo Sigma · OATwo Sigma interview questions
  • Prime Number From a StringAdobe · OAAdobe interview questions
  • Sum Integer StringsIntuit · OAIntuit interview questions
  • Count Times Using at Most Two DigitsLSEG · OALSEG interview questions
  • Valid String CheckUber · OAUber interview questions

Interview reports that mention this question

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public String replaceWithNearestPrime(String s) {
        // Write your code here.
    }
}
Checking account…