Sum two numbers.

What the interviewer wants to learn

  • Whether you understand basic arithmetic operations and data types.
  • If you can translate a simple problem statement into working code quickly.
  • Your ability to discuss edge cases, input validation, and algorithmic complexity even for trivial tasks.
  • How clearly you can explain your solution and the reasoning behind each step.

How to structure your answer

1. **Restate the problem** – briefly paraphrase “add two numbers together.”

2. **Clarify assumptions** – data types, possible ranges, handling of invalid input.

3. **Outline the approach** – describe the algorithm (O(1) addition) and any checks.

4. **Write the code** – a concise snippet in the language of your choice.

5. **Explain complexity** – time O(1), space O(1).

6. **Mention edge cases** – non‑numeric input, overflow, large numbers.

Example answer

“Sure, the task is just to return the sum of two numbers. I’ll assume the inputs are integers that fit within the language’s standard integer type, and I’ll add a quick check to make sure we actually get numbers. In Python that looks like this:

def add(a, b):
    if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
        raise TypeError("Both arguments must be numeric")
    return a + b

The algorithm is straightforward: we perform one addition operation, which is constant‑time, O(1), and it uses only a couple of local variables, so the space usage is also O(1). If we needed to handle very large integers we could rely on Python’s arbitrary‑precision ints, or in a language like Java we’d use `BigInteger` to avoid overflow. That’s the whole solution.”

Mistakes to avoid

  • **Skipping input validation** – the interviewer may probe what happens with non‑numeric or null values.
  • **Talking in abstract without code** – for a coding question they expect to see a concrete implementation.
  • **Over‑engineering** – adding unnecessary loops or data structures for a simple addition confuses the solution.
  • **Ignoring complexity** – even trivial problems should be accompanied by a brief O(1) time/space discussion.
  • **Using language‑specific shortcuts without explanation** – e.g., `eval` or built‑in `sum` without stating why it’s appropriate.