Add Strings
Add Strings 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 non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.
Example 1: Input: num1 = "11", num2 = "123" Output: "134"
Example 2: Input: num1 = "456", num2 = "77" Output: "533"
Example 3: Input: num1 = "0", num2 = "0" Output: "0"
Constraints: 1 <= num1.length, num2.length <= 104 num1 and num2 consist of only digits.
num1 and num2 don't have any leading zeros except for the zero itself.
Follow up: Could you solve it without using any built-in BigInteger library or converting the inputs to integer directly?
Topics and companies
Topics: String.
Reported in interviews at Facebook, Adobe, Google, Wayfair, Microsoft, Square, Uber, Bloomberg, Amazon, Oracle.
How to practise Add Strings 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 Strings in the code editor, or read the Add Strings editorial for a worked solution with its approach and complexity analysis.
Where Add Strings 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 Strings
Other problems that use the same Multiply Strings (Manual Simulation) and String Manipulation Patterns techniques:
- Find Beautiful Indices in the Given Array II — Hard, same pattern
- Find the Index of the First Occurrence in a String — Easy, same pattern
- Integer to Roman — Medium, same pattern
- Palindrome Number — Easy, same pattern
- Repeated String Match — Medium, same pattern
- Repeated Substring Pattern — Easy, same pattern
Preparing your workspace
Progress is saved as you study.