Add Two Arbitrarily Long Binary Strings Digit by Digit

Last updated: November 29, 2026

Quick Overview

Given two strings of 0s and 1s that may be thousands of digits long, return their sum as a binary string without converting the whole inputs to integers.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given two strings of 0s and 1s that may be thousands of digits long, return their sum as a binary string without converting the whole inputs to integers.

A Microsoft phone screen staple that checks clean loop control with two pointers and a carry, the same pattern used in big-number libraries. Interviewers explicitly disallow `int(a, 2)` to see the manual approach.

What the Interviewer Expects
  • Walk both strings from the right end with two indexes
  • Maintain a carry and continue while digits or carry remain
  • Build the output in reverse and flip once at the end
  • Avoid repeated string concatenation in a loop
  • State O(max(n, m)) time
Key Topics to Cover
two pointers
carry propagation
strings
binary arithmetic
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you generalize to any base from 2 to 36?
  • How would you subtract or multiply two binary strings?
  • How would you add a stream of digits arriving least significant first?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Inputs are non-empty strings of 0 and 1 with no leading zeros except the value 0 itself. Lengths can differ and can be up to 10^4. Return the sum in the same format. No conversion of the full st...

High-Level Design

This is grade-school addition. Start at the rightmost digit of each string, add the two digits and the carry, emit the low bit of the total, and pass the high bit on as the next carry. When one string...


Submit Your Answer
Markdown supported