Maximize weighted subsequence pairs with wildcards
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates combinatorial counting, string manipulation, and optimization under constraints, along with handling large-number results via modular arithmetic.
Part 1: Maximize weighted subsequence pair errors with wildcards
Constraints
- 1 <= len(s) <= 2000
- s contains only '0', '1', and '!'
- 1 <= x, y <= 10^9
Examples
Input: ("101!1", 3, 2)
Expected Output:
Explanation: Set ! to 0, giving 10101. Then there are 3 pairs of 01 and 3 pairs of 10, so the score is 3*3 + 3*2 = 15.
Input: ("!", 5, 7)
Expected Output:
Explanation: A single character has no index pair, so the answer is 0.
Input: ("!!", 4, 7)
Expected Output:
Explanation: Choosing 10 gives one 10 pair worth 7. Other assignments give at most 4 or 0.
Input: ("1!!0", 2, 5)
Expected Output:
Explanation: Choosing 1100 gives four 10 subsequence pairs and no better assignment exists, so the score is 4*5 = 20.
Hints
- Process the string from left to right. When you place the current bit, its contribution depends only on how many 0s and 1s already exist before it.