‹ DS&A interview · Socratic
DSA · Math / Bit Manipulation · #33

Add Binary

Module 41 · difficulty 2/5·30:00starts on first keystroke

Given two binary strings `a` and `b`, return their sum as a binary string. Implement the function `addBinary(a, b)` where `a` and `b` consist only of the characters `'0'` and `'1'`. Each string has no leading zeros except when the string is exactly `"0"`. Return the result with no extra leading zeros.

Examples
  • a = "11", b = "1" "100"3 + 1 = 4, which is 100 in binary.
  • a = "1010", b = "1011" "10101"10 + 11 = 21, which is 10101 in binary.
  • a = "0", b = "0" "0"
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.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.