Compute sum and mean over positional sub-ranges of a vector in base R

Quick Overview

Implement base R functions that generate a sequence and return the sum and mean of 1-based inclusive sub-ranges of a vector, stopping on invalid indices. It tests how R indexing behaves at the edges, returning a named list, and mapping one helper over many ranges with lapply.

Compute sum and mean over positional sub-ranges of a vector in base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement three base R functions that generate a numeric sequence and compute the sum and mean over positional sub-ranges of a vector. Use base R only, with no external packages. - `generate_sequence(from, to, by)` returns `seq(from, to, by)`. - `subrange_stats(x, start, end)` takes 1-based inclusive positions and returns a list with exactly the names `sum` and `mean` (numeric scalars) computed over `x[start:end]`. - `subrange_stats` calls `stop()` if `start < 1`, `end > length(x)`, or `start > end`. - `summarize_subranges(x, ranges)` takes a list of `c(start, end)` pairs and returns a list of the same length holding the `subrange_stats` result for each pair. For example: ```r x <- generate_sequence(1, 10, 1) # c(1, 2, ..., 10) subrange_stats(x, 2, 4) # $sum -> 9 (2 + 3 + 4) # $mean -> 3 summarize_subranges(x, list(c(1, 3), c(5, 5))) # [[1]] $sum 6, $mean 2 # [[2]] $sum 5, $mean 5 ``` ```hint Validate before slicing Think about what `x[start:end]` returns when `end` is past the last element or `start > end`, and whether R raises an error in either case. ``` ### Constraints & Assumptions - `x` is a numeric vector. `from`, `to`, `by`, `start` and `end` are numeric scalars. - Each element of `ranges` is a length-2 numeric vector `c(start, end)`. - Positions are 1-based and both ends are inclusive. ### What a Strong Answer Covers - Index validation that runs before the slice and covers all three invalid cases. - What R's indexing would return for each invalid case if the guard were missing. - A return value with exactly the names `sum` and `mean`, each a numeric scalar. - A `summarize_subranges` that reuses `subrange_stats`, so every element has the same shape and goes through the same validation. ### Follow-up Questions - If `summarize_subranges` receives thousands of ranges over a long vector, how would you answer each sum query in constant time after one pass over `x`? - How should `subrange_stats` behave when `x` contains `NA`, and how would you let the caller choose? - How would you return the results as a data frame with one row per range instead of a list?

Overview: Implement base R functions that generate a sequence and return the sum and mean of 1-based inclusive sub-ranges of a vector, stopping on invalid indices. It tests how R indexing behaves at the edges, returning a named list, and mapping one helper over many ranges with lapply.

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

Implement three base R functions that generate a numeric sequence and compute the sum and mean over positional sub-ranges of a vector. Use base R only, with no external packages.

  • generate_sequence(from, to, by) returns seq(from, to, by) .
  • subrange_stats(x, start, end) takes 1-based inclusive positions and returns a list with exactly the names sum and mean (numeric scalars) computed over x[start:end] .
  • subrange_stats calls stop() if start < 1 , end > length(x) , or start > end .
  • summarize_subranges(x, ranges) takes a list of c(start, end) pairs and returns a list of the same length holding the subrange_stats result for each pair.

For example:

x <- generate_sequence(1, 10, 1)   # c(1, 2, ..., 10)
subrange_stats(x, 2, 4)
# $sum  -> 9   (2 + 3 + 4)
# $mean -> 3
summarize_subranges(x, list(c(1, 3), c(5, 5)))
# [[1]] $sum 6, $mean 2
# [[2]] $sum 5, $mean 5

Constraints & Assumptions

  • x is a numeric vector. from , to , by , start and end are numeric scalars.
  • Each element of ranges is a length-2 numeric vector c(start, end) .
  • Positions are 1-based and both ends are inclusive.

What a Strong Answer Covers Guidance

  • Index validation that runs before the slice and covers all three invalid cases.
  • What R's indexing would return for each invalid case if the guard were missing.
  • A return value with exactly the names sum and mean , each a numeric scalar.
  • A summarize_subranges that reuses subrange_stats , so every element has the same shape and goes through the same validation.

Follow-up Questions Guidance

  • If summarize_subranges receives thousands of ranges over a long vector, how would you answer each sum query in constant time after one pass over x ?
  • How should subrange_stats behave when x contains NA , and how would you let the caller choose?
  • How would you return the results as a data frame with one row per range instead of a list?