Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
num1 and num2, return the sum of the two integers.
Example 1:
Input: num1 = 12, num2 = 5 Output: 17 Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
Example 2:
Input: num1 = -10, num2 = 4 Output: -6 Explanation: num1 + num2 = -6, so -6 is returned.
Constraints:
-100 <= num1, num2 <= 100Problem summary: Given two integers num1 and num2, return the sum of the two integers. Example 1: Input: num1 = 12, num2 = 5 Output: 17 Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned. Example 2: Input: num1 = -10, num2 = 4 Output: -6 Explanation: num1 + num2 = -6, so -6 is returned.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
12 5
-10 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2235: Add Two Integers
class Solution {
public int sum(int num1, int num2) {
return num1 + num2;
}
}
// Accepted solution for LeetCode #2235: Add Two Integers
func sum(num1 int, num2 int) int {
return num1 + num2
}
# Accepted solution for LeetCode #2235: Add Two Integers
class Solution:
def sum(self, num1: int, num2: int) -> int:
return num1 + num2
// Accepted solution for LeetCode #2235: Add Two Integers
impl Solution {
pub fn sum(num1: i32, num2: i32) -> i32 {
num1 + num2
}
}
// Accepted solution for LeetCode #2235: Add Two Integers
function sum(num1: number, num2: number): number {
return num1 + num2;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.