Add Binary Strings

EasyStringsstring arithmetic

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given two strings a and b that each represent a non-negative integer written in **base 2**, return the binary string that represents their sum. Each input string contains only the characters '0' and '1'. You must perform the addition directly on the string representations and return the result as a binary string. Do not rely on converting the entire inputs to a fixed-width integer type, since the operands may be longer than a built-in integer can hold; you should add the bits column by column while carrying overflow, just like grade-school addition. The returned string should have **no leading zeros**, except that the value 0 itself is represented as the single character "0".

Examples

Input: a = "11", b = "1"
Output: "100"
Binary 11 is 3 and binary 1 is 1. Their sum is 4, which is 100 in binary.
Input: a = "1010", b = "1011"
Output: "10101"
Binary 1010 is 10 and binary 1011 is 11. Their sum is 21, which is 10101 in binary.
Input: a = "0", b = "0"
Output: "0"
Both operands are zero, so the sum is zero, written as the single digit 0.

Constraints

  • 1 <= a.length, b.length <= 10000
  • Each of `a` and `b` consists only of the characters '0' and '1'.
  • Neither `a` nor `b` contains leading zeros, except when the string is exactly "0".

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.