Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
Given two binary strings a and b, return their sum as a binary string.
Example 1:
Input: a = "11", b = "1" Output: "100"
Example 2:
Input: a = "1010", b = "1011" Output: "10101"
Constraints:
1 <= a.length, b.length <= 104a and b consist only of '0' or '1' characters.Problem summary: Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101"
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Bit Manipulation
"11" "1"
"1010" "1011"
add-two-numbers)multiply-strings)plus-one)add-to-array-form-of-integer)class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry != 0) {
int sum = carry;
if (i >= 0) sum += a.charAt(i--) - '0';
if (j >= 0) sum += b.charAt(j--) - '0';
sb.append(sum % 2);
carry = sum / 2;
}
return sb.reverse().toString();
}
}
func addBinary(a string, b string) string {
i, j, carry := len(a)-1, len(b)-1, 0
out := make([]byte, 0)
for i >= 0 || j >= 0 || carry != 0 {
sum := carry
if i >= 0 {
sum += int(a[i] - '0')
i--
}
if j >= 0 {
sum += int(b[j] - '0')
j--
}
out = append(out, byte(sum%2)+'0')
carry = sum / 2
}
for l, r := 0, len(out)-1; l < r; l, r = l+1, r-1 {
out[l], out[r] = out[r], out[l]
}
return string(out)
}
class Solution:
def addBinary(self, a: str, b: str) -> str:
i, j = len(a) - 1, len(b) - 1
carry = 0
out = []
while i >= 0 or j >= 0 or carry:
total = carry
if i >= 0:
total += ord(a[i]) - ord('0')
i -= 1
if j >= 0:
total += ord(b[j]) - ord('0')
j -= 1
out.append(str(total % 2))
carry = total // 2
return ''.join(reversed(out))
impl Solution {
pub fn add_binary(a: String, b: String) -> String {
let bytes_a = a.as_bytes();
let bytes_b = b.as_bytes();
let mut i = bytes_a.len() as i32 - 1;
let mut j = bytes_b.len() as i32 - 1;
let mut carry = 0;
let mut out: Vec<u8> = Vec::new();
while i >= 0 || j >= 0 || carry != 0 {
let mut sum = carry;
if i >= 0 {
sum += (bytes_a[i as usize] - b'0') as i32;
i -= 1;
}
if j >= 0 {
sum += (bytes_b[j as usize] - b'0') as i32;
j -= 1;
}
out.push((sum % 2) as u8 + b'0');
carry = sum / 2;
}
out.reverse();
String::from_utf8(out).unwrap()
}
}
function addBinary(a: string, b: string): string {
let i = a.length - 1;
let j = b.length - 1;
let carry = 0;
const out: string[] = [];
while (i >= 0 || j >= 0 || carry !== 0) {
let sum = carry;
if (i >= 0) sum += a.charCodeAt(i--) - 48;
if (j >= 0) sum += b.charCodeAt(j--) - 48;
out.push(String(sum % 2));
carry = Math.floor(sum / 2);
}
return out.reverse().join('');
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.