Build and Inspect a Mixed-Type Named List in R

Quick Overview

Write three base R functions that build a named list holding values of different types, report the class of each element, and look up an element by name with an error when it is missing. It tests list indexing with [[, [ and $, class() versus typeof(), and how NULL values affect lookups.

Build and Inspect a Mixed-Type Named List in R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement three base R functions that build and inspect a mixed-type list. No external packages. - `make_mixed_list()` takes no arguments and returns a named list with exactly these 5 elements, in order: `name = "Alice"`, `age = 30L`, `score = 95.5`, `active = TRUE`, `scores = c(88, 92, 79)`. - `element_types(lst)` returns a named character vector of `class(element)` (the first class string) for each element, named by `names(lst)`. - `get_element(lst, name)` returns the element named `name` and calls `stop()` if it is absent. Example: ```r lst <- make_mixed_list() element_types(lst) # name age score active scores # "character" "integer" "numeric" "logical" "numeric" get_element(lst, "name") # "Alice" get_element(lst, "nope") # Error: raised via stop() ``` ```hint Lookup pitfalls Compare what `lst["age"]`, `lst[["age"]]` and `lst$ag` return, and what each one gives you for a name that is not in the list. ``` ### What a Strong Answer Covers - Building the list with the exact names, order and types, including the integer literal. - A per-element class lookup that always yields one string per element and keeps the names. - A name lookup that returns the element itself and fails loudly when the name is absent. - How partial matching, multi-class objects and `NULL` values affect these helpers. ### Follow-up Questions - When do `class()` and `typeof()` disagree, and which should you use here? - How would you make `get_element` distinguish a missing name from an element whose value is `NULL`? - What does `get_element` return if two elements share the same name, and how would you detect that case?

Overview: Write three base R functions that build a named list holding values of different types, report the class of each element, and look up an element by name with an error when it is missing. It tests list indexing with [[, [ and $, class() versus typeof(), and how NULL values affect lookups.

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

Implement three base R functions that build and inspect a mixed-type list. No external packages.

  • make_mixed_list() takes no arguments and returns a named list with exactly these 5 elements, in order: name = "Alice" , age = 30L , score = 95.5 , active = TRUE , scores = c(88, 92, 79) .
  • element_types(lst) returns a named character vector of class(element) (the first class string) for each element, named by names(lst) .
  • get_element(lst, name) returns the element named name and calls stop() if it is absent.

Example:

lst <- make_mixed_list()
element_types(lst)
#     name      age    score   active   scores
# "character" "integer" "numeric" "logical" "numeric"

get_element(lst, "name")   # "Alice"
get_element(lst, "nope")   # Error: raised via stop()

What a Strong Answer Covers Guidance

  • Building the list with the exact names, order and types, including the integer literal.
  • A per-element class lookup that always yields one string per element and keeps the names.
  • A name lookup that returns the element itself and fails loudly when the name is absent.
  • How partial matching, multi-class objects and NULL values affect these helpers.

Follow-up Questions Guidance

  • When do class() and typeof() disagree, and which should you use here?
  • How would you make get_element distinguish a missing name from an element whose value is NULL ?
  • What does get_element return if two elements share the same name, and how would you detect that case?