Implement four sequence-building helpers in base R

Quick Overview

Write four base R helpers that build numeric vectors: a stepped sequence, the first n positive even numbers, a repeated vector and a countdown by 1. It tests whether you know seq, seq_len and rep well enough to get the bounds right and return an empty vector when n is 0.

Implement four sequence-building helpers in base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement four helpers in base R (no packages) that build numeric vectors. - `make_sequence(from, to, by)` returns `seq(from, to, by)`. - `even_numbers(n)` returns the first `n` positive even numbers (2, 4, 6, ...). When `n == 0` it returns `numeric(0)`. - `repeat_vector(x, times)` returns `rep(x, times)`. - `reverse_sequence(from, to)`, where `from >= to`, counts down from `from` to `to` by 1. For example: ```r make_sequence(1, 10, 2) # 1 3 5 7 9 even_numbers(4) # 2 4 6 8 repeat_vector(c(1, 2), 3) # 1 2 1 2 1 2 reverse_sequence(5, 1) # 5 4 3 2 1 ``` ```hint Check the empty case Work out what `1:n` returns when `n` is 0 before you use it to build the even numbers. ``` ### Constraints & Assumptions - All arguments are scalars except `x`, which is a numeric vector. - Inputs are valid: `n` is non-negative, `times` is positive, and `from >= to` in `reverse_sequence`. No error handling is required. - Every helper returns a numeric vector. ### What a Strong Answer Covers - The base R constructor that matches each helper's step and length semantics, with no explicit loops. - An `even_numbers` that returns an empty numeric vector for `n == 0`, and the reason the naive form fails there. - A countdown in `reverse_sequence` that uses the right step sign and still works when `from == to`. - The difference between tiling a whole vector and repeating each element in place. ### Follow-up Questions - What is the difference between `rep(x, times = 3)`, `rep(x, each = 3)` and `rep(x, length.out = 5)`? - Why are `seq_len(n)` and `seq_along(x)` safer than `1:n` and `1:length(x)` in loops? - How would you change `reverse_sequence` so it accepts `from < to` and counts in whichever direction is needed?

Overview: Write four base R helpers that build numeric vectors: a stepped sequence, the first n positive even numbers, a repeated vector and a countdown by 1. It tests whether you know seq, seq_len and rep well enough to get the bounds right and return an empty vector when n is 0.

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

Implement four helpers in base R (no packages) that build numeric vectors.

  • make_sequence(from, to, by) returns seq(from, to, by) .
  • even_numbers(n) returns the first n positive even numbers (2, 4, 6, ...). When n == 0 it returns numeric(0) .
  • repeat_vector(x, times) returns rep(x, times) .
  • reverse_sequence(from, to) , where from >= to , counts down from from to to by 1.

For example:

make_sequence(1, 10, 2)   # 1 3 5 7 9
even_numbers(4)           # 2 4 6 8
repeat_vector(c(1, 2), 3) # 1 2 1 2 1 2
reverse_sequence(5, 1)    # 5 4 3 2 1

Constraints & Assumptions

  • All arguments are scalars except x , which is a numeric vector.
  • Inputs are valid: n is non-negative, times is positive, and from >= to in reverse_sequence . No error handling is required.
  • Every helper returns a numeric vector.

What a Strong Answer Covers Guidance

  • The base R constructor that matches each helper's step and length semantics, with no explicit loops.
  • An even_numbers that returns an empty numeric vector for n == 0 , and the reason the naive form fails there.
  • A countdown in reverse_sequence that uses the right step sign and still works when from == to .
  • The difference between tiling a whole vector and repeating each element in place.

Follow-up Questions Guidance

  • What is the difference between rep(x, times = 3) , rep(x, each = 3) and rep(x, length.out = 5) ?
  • Why are seq_len(n) and seq_along(x) safer than 1:n and 1:length(x) in loops?
  • How would you change reverse_sequence so it accepts from < to and counts in whichever direction is needed?