Skip to main content
All Questions

Find the minimum operations to balance 0s and 1s after merging two binary strings.

Unlock detailed company stats for this questionUpgrade

Function minOperationsToUnbias(data1, data2) returns an int. Given two binary strings data1 and data2, in one operation you can remove the rightmost character of data1 or the leftmost character of data2. Find the minimum number of operations so that after merging, the total number of 0s equals the total number of 1s. The datasets need not be the same size when merged; the combined dataset must just contain an equal number of 0s and 1s. Example: data1 = "001", data2 = "110" -> 2 (remove both 1s from data2, or the last 1 of data1 and first 1 of data2). Example: data1 = "111000", data2 = "001111" -> 5. Constraints: 1 <= |data1|, |data2| <= 10^5.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.