R Data Structures and List Access

Quick Overview

Write four base R functions that build a numeric vector and a column-filled matrix, nest them with another list inside a named list, and pull the matrix back out by name. It tests base R constructors, column-major fill order, and how list indexing by name works.

R Data Structures and List Access

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement four base R functions that build a numeric vector and a matrix, combine them with an existing list into a named list, and retrieve a component by name. - `make_vector(values)`: return `as.numeric(values)`. - `make_matrix(values, nrow, ncol)`: return a matrix filled column-wise (`byrow = FALSE`) with the given dimensions. - `combine_into_list(vec, mat, extra)`: return a length-3 list named `c("vector", "matrix", "list")` holding `vec`, `mat`, `extra`. - `access_matrix(container)`: return the element named `"matrix"`. Example: ```r v <- make_vector(c(1, 2, 3)) m <- make_matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) lst <- combine_into_list(v, m, list(a = 1)) access_matrix(lst) # returns m: # [,1] [,2] # [1,] 1 3 # [2,] 2 4 ``` ```hint Check what you extract Call `is.matrix()` on whatever `access_matrix()` returns. A one-element list that holds the matrix is not the matrix. ``` ### What a Strong Answer Covers - Builds each structure with the right base constructor: `as.numeric()` for the vector and `matrix()` with the given dimensions and column-wise fill. - Returns a named list of exactly three elements, with `extra` nested as a single element and not spliced into the outer list. - Retrieves the matrix by name in a way that returns the matrix object itself. - Says what happens on awkward input, such as a `values` length that does not equal `nrow * ncol` or a name that is missing from the list. ### Follow-up Questions - How does `byrow = TRUE` change the matrix layout? - Why is `container$matrix` less safe than `container[["matrix"]]` when the list's names are not under your control? - What does `as.numeric(factor(c("10", "20")))` return, and how do you get the numbers 10 and 20? - If `extra` is `NULL`, does the list still have length 3?

Overview: Write four base R functions that build a numeric vector and a column-filled matrix, nest them with another list inside a named list, and pull the matrix back out by name. It tests base R constructors, column-major fill order, and how list indexing by name works.

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

Implement four base R functions that build a numeric vector and a matrix, combine them with an existing list into a named list, and retrieve a component by name.

  • make_vector(values) : return as.numeric(values) .
  • make_matrix(values, nrow, ncol) : return a matrix filled column-wise ( byrow = FALSE ) with the given dimensions.
  • combine_into_list(vec, mat, extra) : return a length-3 list named c("vector", "matrix", "list") holding vec , mat , extra .
  • access_matrix(container) : return the element named "matrix" .

Example:

v <- make_vector(c(1, 2, 3))
m <- make_matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
lst <- combine_into_list(v, m, list(a = 1))
access_matrix(lst)
# returns m:
#      [,1] [,2]
# [1,]    1    3
# [2,]    2    4

What a Strong Answer Covers Guidance

  • Builds each structure with the right base constructor: as.numeric() for the vector and matrix() with the given dimensions and column-wise fill.
  • Returns a named list of exactly three elements, with extra nested as a single element and not spliced into the outer list.
  • Retrieves the matrix by name in a way that returns the matrix object itself.
  • Says what happens on awkward input, such as a values length that does not equal nrow * ncol or a name that is missing from the list.

Follow-up Questions Guidance

  • How does byrow = TRUE change the matrix layout?
  • Why is container$matrix less safe than container[["matrix"]] when the list's names are not under your control?
  • What does as.numeric(factor(c("10", "20"))) return, and how do you get the numbers 10 and 20?
  • If extra is NULL , does the list still have length 3?