Build and inspect a student scores data frame in base R with pass flags and letter grades
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement four base-R helper functions in `/app/solution.R` for building and inspecting a data frame of student scores. No external packages are allowed.
- `make_dataframe(names, scores)` takes a character vector `names` and a numeric vector `scores` of equal length and returns a data.frame with columns `name` (character), `score` (numeric) and `passed` (logical, `TRUE` when `score >= 50`). Build it with `stringsAsFactors = FALSE`, and call `stop()` if the two lengths differ.
- `df_dimensions(df)` returns the named vector `c(rows = <nrow>, cols = <ncol>)`.
- `add_grade_column(df)` adds a character column `grade` derived from `score` and returns the frame: `"A"` for `>= 90`, `"B"` for `>= 80`, `"C"` for `>= 70`, `"D"` for `>= 60`, and `"F"` otherwise.
- `column_types(df)` returns a character vector holding `class(df[[col]])[1]` for each column, named by the column names.
Character columns must never be converted to factors.
For example:
```r
df <- make_dataframe(c("Ann", "Bo"), c(91, 40))
# name score passed
# 1 Ann 91 TRUE
# 2 Bo 40 FALSE
df_dimensions(df) # c(rows = 2, cols = 3)
add_grade_column(df)$grade # c("A", "F")
column_types(df) # c(name = "character", score = "numeric", passed = "logical")
```
```hint Boundary scores
Each score must land in exactly one band. Look at the order in which the thresholds are checked, and trace a score that sits exactly on a boundary such as 60 or 90.
```
### Constraints & Assumptions
- Base R only, with no packages loaded.
- `names` and `scores` can arrive with different lengths, and that case must raise an error.
- Every lower threshold is inclusive: a score of 90 is an "A" and a score of 50 passes.
- The column types must come out the same whatever the running R version's default for `stringsAsFactors` is.
### What a Strong Answer Covers
- How mismatched input lengths are detected and reported before the frame is built.
- How `passed` and `grade` are computed over the whole column at once, without a row loop.
- How the grade checks are ordered so boundary scores land in the right band.
- How `df_dimensions()` and `column_types()` return named vectors, including for columns whose class has more than one element.
- Why `name` stays a character column on any R version.
### Follow-up Questions
- How would you write the grade assignment with `cut()` instead of nested `ifelse()`, and how do you make sure a score of exactly 90 still gets an "A"?
- Why does `stringsAsFactors = FALSE` matter here, and what changed in R 4.0?
- What does each function return for zero-length input, or for a score that is `NA`?
- Why is `vapply()` a safer choice than `sapply()` inside `column_types()`?
Overview: Write four base-R helpers that build a data frame from names and scores, report its dimensions, add a letter grade column and list each column's class. It tests vectorized conditionals, named vectors, boundary handling in grade bands and keeping character columns from turning into factors.