Reproduce Base R Vector Semantics: Recycling, Coercion, seq, Indexing and rep

Quick Overview

Implement five base R functions that return exactly what R produces for vector recycling, type coercion in c(), seq with a step, indexing with negative positions, and rep. It tests whether you know how R vectors behave, including uneven lengths, mixed types, steps that miss the endpoint and out-of-range indices.

Reproduce Base R Vector Semantics: Recycling, Coercion, seq, Indexing and rep

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement five functions in base R (no external packages). Each must return exactly what R produces for the described expression. - `vector_recycle(a, b)`: `a + b` with recycling (one length is a multiple of the other). - `combine_type(x, y)`: the length-1 string `typeof(c(x, y))`, following the order `logical < integer < double < character`. - `seq_values(from, to, by)`: `seq(from, to, by = by)`. - `subset_vector(x, idx)`: `x[idx]`, where negative indices exclude positions. - `rep_pattern(x, times)`: `rep(x, times = times)`. For example: ```r vector_recycle(c(1, 2, 3, 4), c(10, 20)) # 11 22 13 24 combine_type(1L, "a") # "character" seq_values(1, 9, 2) # 1 3 5 7 9 subset_vector(c(10, 20, 30), -2L) # 10 30 rep_pattern(c(1, 2), 3L) # 1 2 1 2 1 2 ``` ```hint Know the rule behind each call For each function, first find the base R expression that already gives the answer. Then work out by hand what it returns for inputs that tend to surprise people: lengths that do not divide evenly, an `NA` mixed into `c()`, a step that does not land on `to`, a zero or out-of-range index. ``` ### What a Strong Answer Covers - How recycling lines up the elements of two vectors of different lengths, and what R does when the longer length is not a multiple of the shorter. - The coercion order `c()` follows and the type it returns for mixed inputs. - Where `seq` stops when the step does not land on `to`, and which type it returns. - What negative, zero, out-of-range and `NA` indices do. - How `rep` with `times` differs from `each` and `length.out`. ### Follow-up Questions - What does `c(1, 2, 3) + c(10, 20)` return, and what else does R print? - What do `typeof(c(TRUE, NA))`, `typeof(c(NA, 1L))` and `typeof(c(1L, 2.5))` return? - Why is `seq(0, 1, by = 0.1)[4] == 0.3` `FALSE`, and how should you compare such values? - What do `subset_vector(c(10, 20, 30), c(-1, 2))` and `subset_vector(c(10, 20, 30), 5)` return?

Overview: Implement five base R functions that return exactly what R produces for vector recycling, type coercion in c(), seq with a step, indexing with negative positions, and rep. It tests whether you know how R vectors behave, including uneven lengths, mixed types, steps that miss the endpoint and out-of-range indices.

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

Implement five functions in base R (no external packages). Each must return exactly what R produces for the described expression.

  • vector_recycle(a, b) : a + b with recycling (one length is a multiple of the other).
  • combine_type(x, y) : the length-1 string typeof(c(x, y)) , following the order logical < integer < double < character .
  • seq_values(from, to, by) : seq(from, to, by = by) .
  • subset_vector(x, idx) : x[idx] , where negative indices exclude positions.
  • rep_pattern(x, times) : rep(x, times = times) .

For example:

vector_recycle(c(1, 2, 3, 4), c(10, 20))  # 11 22 13 24
combine_type(1L, "a")                     # "character"
seq_values(1, 9, 2)                       # 1 3 5 7 9
subset_vector(c(10, 20, 30), -2L)         # 10 30
rep_pattern(c(1, 2), 3L)                  # 1 2 1 2 1 2

What a Strong Answer Covers Guidance

  • How recycling lines up the elements of two vectors of different lengths, and what R does when the longer length is not a multiple of the shorter.
  • The coercion order c() follows and the type it returns for mixed inputs.
  • Where seq stops when the step does not land on to , and which type it returns.
  • What negative, zero, out-of-range and NA indices do.
  • How rep with times differs from each and length.out .

Follow-up Questions Guidance

  • What does c(1, 2, 3) + c(10, 20) return, and what else does R print?
  • What do typeof(c(TRUE, NA)) , typeof(c(NA, 1L)) and typeof(c(1L, 2.5)) return?
  • Why is seq(0, 1, by = 0.1)[4] == 0.3 FALSE , and how should you compare such values?
  • What do subset_vector(c(10, 20, 30), c(-1, 2)) and subset_vector(c(10, 20, 30), 5) return?