Write an element-wise add_numbers function in R that follows vector recycling rules

Quick Overview

Write an R function that adds two numeric vectors element by element and follows R's recycling rules when their lengths differ. It tests vectorized arithmetic, how scalars behave as length-1 vectors, and edge cases such as NA values, empty inputs and mismatched lengths.

Write an element-wise add_numbers function in R that follows vector recycling rules

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `add_numbers(a, b)` in R. It takes two numeric vectors (or scalars) and returns their element-wise sum as a numeric vector. Requirements: - Two scalar inputs return a length-1 numeric. - When the lengths differ, follow R's standard recycling rules. Examples: ```r add_numbers(2, 3) # 5 add_numbers(c(1, 2, 3), 10) # 11 12 13 add_numbers(c(1, 2), c(3, 4)) # 4 6 ``` ```hint Check the operators first Before writing a loop, look at how R's arithmetic operators treat two vectors, including vectors of different lengths. ``` ### Clarifying Questions to Ask - When the longer length is not a multiple of the shorter one, is base R's warning enough, or should the function stop with an error? - Should non-numeric input such as character vectors or factors be rejected with a clear message? - What should the function return when one input has length zero? ### What a Strong Answer Covers - Uses R's vectorized arithmetic and explains why no explicit loop or length handling is needed - States the recycling rule, including the warning when the lengths do not divide evenly - Explains that a scalar in R is a length-1 vector, which is why the scalar case needs no special code - Handles edge cases: NA values, zero-length input, non-numeric arguments - Gives the time and memory cost in terms of the input lengths ### Follow-up Questions - How would you change the function so that mismatched lengths raise an error instead of a warning? - How would you add a list of ten equal-length vectors element-wise? - What happens when both inputs are integers and a sum exceeds `.Machine$integer.max`? - How does `add_numbers()` differ from `sum()`, and when would you use each?

Overview: Write an R function that adds two numeric vectors element by element and follows R's recycling rules when their lengths differ. It tests vectorized arithmetic, how scalars behave as length-1 vectors, and edge cases such as NA values, empty inputs and mismatched lengths.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement add_numbers(a, b) in R. It takes two numeric vectors (or scalars) and returns their element-wise sum as a numeric vector.

Requirements:

  • Two scalar inputs return a length-1 numeric.
  • When the lengths differ, follow R's standard recycling rules.

Examples:

add_numbers(2, 3)             # 5
add_numbers(c(1, 2, 3), 10)   # 11 12 13
add_numbers(c(1, 2), c(3, 4)) # 4 6

Clarifying Questions to Ask Guidance

  • When the longer length is not a multiple of the shorter one, is base R's warning enough, or should the function stop with an error?
  • Should non-numeric input such as character vectors or factors be rejected with a clear message?
  • What should the function return when one input has length zero?

What a Strong Answer Covers Guidance

  • Uses R's vectorized arithmetic and explains why no explicit loop or length handling is needed
  • States the recycling rule, including the warning when the lengths do not divide evenly
  • Explains that a scalar in R is a length-1 vector, which is why the scalar case needs no special code
  • Handles edge cases: NA values, zero-length input, non-numeric arguments
  • Gives the time and memory cost in terms of the input lengths

Follow-up Questions Guidance

  • How would you change the function so that mismatched lengths raise an error instead of a warning?
  • How would you add a list of ten equal-length vectors element-wise?
  • What happens when both inputs are integers and a sum exceeds .Machine$integer.max ?
  • How does add_numbers() differ from sum() , and when would you use each?