FastPrepMinimum Value Calculation by Replacing '!' in a Binary String
Problems/Minimum Value Calculation by Replacing '!' in a Binary String

Minimum Value Calculation by Replacing '!' in a Binary String

Amazon logoAmazon● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a string s made of the characters '0', '1' and '!'. You must replace each '!' with either '0' or '1'. You are also given two integers x and y.

After the replacement, look at every pair of characters that sit next to each other:

  • Each pair "01" adds x to the total.
  • Each pair "10" adds y to the total.
  • Pairs "00" and "11" add nothing.

Return the smallest total you can get.

For example, let s = "1!1", x = 2 and y = 5. Replace '!' with '1' to get "111". It has no "01" or "10" pair. The answer is 0.

Function

calculateMinimumValue(s: String, x: int, y: int) → int

Complete the function calculateMinimumValue in the editor.

calculateMinimumValue has the following parameters:

  • String s: the string of '0', '1' and '!' characters
  • int x: the amount added for each "01" pair
  • int y: the amount added for each "10" pair

Returns int: the smallest total after replacing every '!'

Examples

Example 1

s = "01!0"x = 2y = 3return = 5

Replace '!' with '0' to get "0100". Its pairs are "01", "10" and "00", so the total is x + y = 2 + 3 = 5. Replace '!' with '1' to get "0110". Its pairs are "01", "11" and "10", so the total is also 2 + 3 = 5. The answer is 5.

Example 2

s = "!0!1"x = 3y = 4return = 3

Replace both '!' with '0' to get "0001". Its pairs are "00", "00" and "01", so the total is x = 3. You cannot do better: the '0' at index 1 comes before the '1' at index 3, so some pair between them must be "01". The answer is 3.

Constraints

  • 1 <= s.length <= 10^5
  • Every character of s is '0', '1' or '!'
  • s has at least one '!'
  • 0 <= x <= 10^4
  • 0 <= y <= 10^4

More Amazon interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026

Similar questions at other companies

  • Longest Subsequence That Is a SubstringBooking.com · OA
  • Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
  • Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions
  • Longest String ChainVisa · OAVisa interview questions
  • Minimum-Cost Digit String DecodingSquadStack.ai · OA
  • Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
  • Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
  • Minimum Edit-Distance String for Each QueryTarget · OA
  • Longest Subsequence That Is a SubstringWells Fargo · OAWells Fargo interview questions
  • Non-Alternating Binary PartitionsThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int calculateMinimumValue(String s, int x, int y) {
      // write your code here
    }

}
Checking account…