Skip to main content

67. Add Binary

easyAsked at Meta

Given two binary strings, return their sum as a binary string. Meta asks this to test whether you can simulate column addition without converting to integers (numbers can exceed int64).

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Meta loops.

  • Glassdoor (2026-Q1)Meta E3 phone-screen reports cite this as a warm-up.
  • Blind (2025-11)Recurring Meta interview question.

Problem

Given two binary strings a and b, return their sum as a binary string.

Constraints

  • 1 <= a.length, b.length <= 10^4
  • a and b consist only of '0' or '1' characters.
  • Each string does not contain leading zeros except for the zero itself.

Examples

Example 1

Input
a = "11", b = "1"
Output
"100"

Example 2

Input
a = "1010", b = "1011"
Output
"10101"

Approaches

1. Convert to numbers, add, re-convert

Parse both as ints, add, format result back as binary.

Time
O(n)
Space
O(n)
function addBinaryConvert(a, b) {
  return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
}

Tradeoff: Cheats by relying on BigInt — won't satisfy 'simulate column addition' graders. Some interviewers explicitly disallow this.

2. Column-by-column addition with carry (optimal)

Walk both strings from right to left. At each position, sum the two digits + carry; emit bit; track new carry.

Time
O(max(m, n))
Space
O(max(m, n))
function addBinary(a, b) {
  let i = a.length - 1, j = b.length - 1, carry = 0;
  const out = [];
  while (i >= 0 || j >= 0 || carry) {
    const da = i >= 0 ? Number(a[i--]) : 0;
    const db = j >= 0 ? Number(b[j--]) : 0;
    const sum = da + db + carry;
    out.push(sum % 2);
    carry = sum >= 2 ? 1 : 0;
  }
  return out.reverse().join('');
}

Tradeoff: Simulates manual binary addition. Works for arbitrarily long strings (the input bound is 10^4 bits — well beyond int64).

Meta-specific tips

Meta interviewers grade this on whether you simulate column addition correctly. The carry chain is the key — it can propagate all the way through. The simulation should look like elementary-school addition: ones column first, with carries flowing left.

Common mistakes

  • Using parseInt or Number — overflows on strings longer than 32 bits.
  • Forgetting the final carry (e.g., 1+1 leaves a carry that becomes a new high bit).
  • Walking left-to-right — carries flow right-to-left, so iterating from the end is mandatory.

Follow-up questions

An interviewer at Meta may pivot to one of these next:

  • Add Strings (LC 415) — same pattern, base 10.
  • Add Two Numbers (LC 2) — same pattern, on linked lists.
  • What if the bases were arbitrary (e.g., base 7)?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why not use parseInt?

JavaScript Numbers are 64-bit doubles with ~53 bits of integer precision. A 10^4-bit binary string overflows. Use BigInt or simulate.

Why is the carry-only case necessary in the loop condition?

If a and b are both exhausted but carry === 1, we still need to emit the final 1. Example: a='1', b='1' → result '10'. Without the carry check, we'd stop at '0'.

Practice these live with InterviewChamp.AI

Drill Add Binary and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →