Question 1
Some developers at Amazon want to merge two binary classification training datasets such that the final dataset is unbiased.
The annotated classification values of the two datasets are represented using two binary strings, data1 and data2 where 0 represents one class and 1 represents another class.
In a single operation, the rightmost data point of data1 can be removed or the leftmost data point of data2 can be removed.
Given data1 and data2, find the minimum number of operations required such that after merging the two data sets, the total number of 0s is equal to the total number of 1s.
Note: The two datasets do not need to be of the same size at the time of merging; the only requirement is that the combined dataset must contain an equal number of 0s and 1s.
Example
Suppose data1 = “001” and data2 = “110”.
It takes 2 operations to make the number of zeros and ones equal. Hence the answer is 2.
Function Description
Complete the function minOperationsToUnbias in the editor below.
minOperationsToUnbias takes the following arguments:
string data1: The classification values of the first dataset
string data2: The classification values of the second dataset
Returns:
int: The minimum operations required so that the total number of 0s is equal to the total number of 1s.
Constraints
1 ≤ |data1|, |data2| ≤ 10⁵
Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
STDIN FUNCTION
3 → data1 = "001"
001
3 → data2 = "110"
110
Sample Output
2
Explanation
It is optimal to remove both the 1s from the second data set or remove the last 1 from data1 and first 1 from data2. Both require two operations. Finally after merging the data the number of 0s and 1s is equal.
Sample Case 1
Sample Input For Custom Testing
STDIN FUNCTION
6 → data1 = “111000”
111000
6 → data2 = “001111”
001111
Sample Output
5
Explanation
Remove the last three 1s from data1 and, in five operations, remove the first five characters of data2 (delete: “11100” and data2 = “1”).