Magicsheet logo

Add Two Integers

Easy
30%
Updated 6/1/2025

Add Two Integers

What is this problem about?

The Add Two Integers interview question is arguably the simplest problem in technical interviews. Given two integers num1 and num2, you are asked to return their sum.

Why is this asked in interviews?

While it seems trivial, it is often used as a "smoke test" or a way to get a candidate comfortable with the coding environment. At companies like Apple or Atlassian, it might be used to discuss bitwise operations (how addition works at the hardware level) or to test basic syntax in a new language.

Algorithmic pattern used

This uses the basic Math interview pattern. In most cases, you simply use the + operator. In advanced variations, you might be asked to solve it using Bit Manipulation (using XOR for addition and AND with a bit shift for carries).

Example explanation

If num1 = 12 and num2 = 5, the sum is 12+5=1712 + 5 = 17. If using bit manipulation:

  • 12 (1100) XOR 5 (0101)=9 (1001)12 \text{ (1100)} \text{ XOR } 5 \text{ (0101)} = 9 \text{ (1001)}
  • 12 AND 5=4 (0100)12 \text{ AND } 5 = 4 \text{ (0100)}, shifted left is 8 (1000)8 \text{ (1000)}
  • Repeat until the carry is 00.

Common mistakes candidates make

  • Overthinking: Looking for a complex trick when the question is genuinely simple.
  • Overflow: In languages like C++, adding two very large int values might exceed the 32-bit limit, though modern interview platforms usually account for this.

Interview preparation tip

If you receive this in a high-stakes interview, assume the interviewer might want to pivot into "How would you do this without the + operator?" Be ready to discuss the bitwise logic of a Half Adder or Full Adder.

Similar Questions