FastPrepMinimize Binary Subsequence Cost
Problems/Minimize Binary Subsequence Cost

Minimize Binary Subsequence Cost

Amazon logoAmazonFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a string binaryString made only of '0', '1', and '!', and two integers x and y.

Replace every '!' with either '0' or '1'. Then add up the cost. Every subsequence equal to "01" costs x, and every subsequence equal to "10" costs y. Such a subsequence is any pair of positions, read left to right, that hold those two characters. The two positions do not have to be next to each other.

Return the smallest total cost you can get, modulo 1_000_000_007.

Function

minimizeBinarySubsequenceCost(binaryString: String, x: int, y: int) → int

Examples

Example 1

binaryString = "101!1"x = 2y = 3return = 9

Replacing '!' with '0' gives cost 15. Replacing it with '1' gives cost 9, which is optimal.

Example 2

binaryString = "!!!!!"x = 2y = 3return = 0

Replace every '!' with the same character, for example all zeros. Then the string has no "01" or "10" subsequences, so the total cost is zero.

Constraints

  • 1 <= binaryString.length <= 10^5
  • 0 <= x, y <= 10^5
  • binaryString contains only '0', '1', and '!'.

More Amazon Online Assessment questions

  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
  • Get Smallest Base SegmentOA · Seen Nov 2026
  • Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
  • Minimum Grid InconvenienceOA · Seen Nov 2026
  • Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
  • Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Nov 2026

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Recent Reports (Part 4 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, Earlier Reports (Part 5 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int minimizeBinarySubsequenceCost(String binaryString, int x, int y) {
      // write your code here
    }
}
Checking account…