Add Strings
Google Interview Question
Poll of the Day
Video Solution (Short)
Video Solution (Detailed)
Problem Overview
Difficulty: Easy
LeetCode Pattern: Two Pointers
Given two integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.
Note: You must not convert the inputs to integers directly.
Input:
· num1 = "456"
· num2 = "77"
Output: "533"Input:
· num1 = "11"
· num2 = "123"
Output: "134"Step 1: Clarify Requirements
Can inputs contain negative numbers?
No.
Can inputs contain leading zeros?
No.
Can inputs be of different lengths?
Yes.
Step 2: Discuss Approaches
Approach 1: Convert to int
Logic:
Convert strings to integers
Add normally
Convert back to string
Big O:
Time Complexity: O(s)
Space Complexity: O(s)
Approach 2: Class Addition (Optimal)
Logic:
Start from ends of both strings
Add digit by digit with carry
Build result string backward
Reverse at the end
Big O:
Time Complexity: O(s)
Space Complexity: O(s)
Step 3: Write Code (Optimal)
Python
Java
C++
Step 4: Answer Follow-Ups
What if input contains non-digit chars?
Add input validation.
Throw an error or return -1.
What if inputs have leading zeros?
Our algorithm still works.
What if you had to subtract strings?
We will use a similar approach.
Just handle borrow carefully.
👨💻 Need Interview Practice?
Book a 1:1 with me
Book Here: Booking URL




