Add Binary
Add Binary is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Multiply Strings (Manual Simulation) subpattern.
Problem statement
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 <= 104 a and b consist only of '0' or '1' characters.
Each string does not contain leading zeros except for the zero itself.
Topics and companies
Topics: Math, String.
Reported in interviews at Facebook, Amazon, ByteDance.
How to practise Add Binary on Thita.ai
Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Add Binary in the code editor, or read the Add Binary editorial for a worked solution with its approach and complexity analysis.
Where Add Binary sits in the DSA pattern sheet
- String Manipulation Patterns pattern guide
- Multiply Strings (Manual Simulation) subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Add Binary
Other problems that use the same Multiply Strings (Manual Simulation) and String Manipulation Patterns techniques:
- Add to Array-Form of Integer — Easy, same subpattern
- Multiply Strings — Medium, same subpattern
- Plus One — Easy, same subpattern
- Diagonal Traverse — Medium, same pattern
- Find All Duplicates in an Array — Medium, same pattern
- Find All Numbers Disappeared in an Array — Easy, same pattern
Preparing your workspace
Progress is saved as you study.