Reproducible Stratified Sampling in Base R With a Fixed Seed

Quick Overview

Implement two base R functions that draw a reproducible stratified sample without replacement from a single seed, taking either a fixed number of rows or a fraction of each stratum. It tests seeding and RNG order, deterministic stratum iteration, and the sample() behavior that can break per-group draws.

Reproducible Stratified Sampling in Base R With a Fixed Seed

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `stratified_sample(df, strata_col, n_per_stratum, seed)` and `stratified_sample_frac(df, strata_col, frac, seed)` in base R. Each draws a reproducible stratified sample without replacement and calls `set.seed(seed)` once, at its start. - `stratified_sample` draws `n_per_stratum` rows per stratum. - `stratified_sample_frac` draws `floor(frac * stratum_size)` rows per stratum, with `frac` in `[0, 1]`. - Iterate over `strata <- sort(unique(as.character(df[[strata_col]])))` in that order. - For each stratum, take its row positions `idx <- which(as.character(df[[strata_col]]) == stratum)` and draw the required number of them without replacement. For a stratum with more than one row, the draw must give the same result as `sample(idx, size)` would from the same random state. - Return `df` subset to the chosen rows, concatenated in strata order (draw order within each stratum), with row names reset to `1..N`. For example: ```r df <- data.frame(grp = c("a", "a", "a", "b", "b"), x = 1:5) stratified_sample(df, "grp", n_per_stratum = 1, seed = 42) # one row from group "a" then one from "b", row.names 1..2 ``` ```hint A stratum with one row Compare `sample(c(4, 5), 1)` with `sample(5, 1)` a few times. Then ask what `idx` looks like when a stratum has exactly one row. ``` ```hint Same seed, same rows The output depends on the order in which the strata consume random numbers. Think about what could change that order, or the numbers themselves, on another machine or R version. ``` ### Clarifying Questions to Ask - Rows whose stratum value is `NA`: should they form their own stratum or be left out? - When a stratum has fewer rows than `n_per_stratum`, should the function stop with an error or return the whole stratum? - Must the sample match across machines with different locales or R versions, or only across runs on the same setup? ### What a Strong Answer Covers - Seeding once and drawing the strata in a fixed order so the random stream is consumed the same way on every run. - Sampling each stratum's row positions without replacement, including strata that have a single row. - Computing the fractional size with `floor`, including sizes of zero. - Building the result in strata order with row names reset to `1..N`. - What can still change the result for the same seed (locale-dependent sorting, R's sampler version). ### Follow-up Questions - Why call `set.seed()` once instead of inside the per-stratum loop? - What happens when `n_per_stratum` exceeds a stratum's size? - `set.seed()` changes the caller's global random state. How would you avoid that side effect? - How would you write this with dplyr, and would it return the same rows for the same seed?

Overview: Implement two base R functions that draw a reproducible stratified sample without replacement from a single seed, taking either a fixed number of rows or a fraction of each stratum. It tests seeding and RNG order, deterministic stratum iteration, and the sample() behavior that can break per-group draws.

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

Implement stratified_sample(df, strata_col, n_per_stratum, seed) and stratified_sample_frac(df, strata_col, frac, seed) in base R. Each draws a reproducible stratified sample without replacement and calls set.seed(seed) once, at its start.

  • stratified_sample draws n_per_stratum rows per stratum.
  • stratified_sample_frac draws floor(frac * stratum_size) rows per stratum, with frac in [0, 1] .
  • Iterate over strata <- sort(unique(as.character(df[[strata_col]]))) in that order.
  • For each stratum, take its row positions idx <- which(as.character(df[[strata_col]]) == stratum) and draw the required number of them without replacement. For a stratum with more than one row, the draw must give the same result as sample(idx, size) would from the same random state.
  • Return df subset to the chosen rows, concatenated in strata order (draw order within each stratum), with row names reset to 1..N .

For example:

df <- data.frame(grp = c("a", "a", "a", "b", "b"), x = 1:5)
stratified_sample(df, "grp", n_per_stratum = 1, seed = 42)
# one row from group "a" then one from "b", row.names 1..2

Clarifying Questions to Ask Guidance

  • Rows whose stratum value is NA : should they form their own stratum or be left out?
  • When a stratum has fewer rows than n_per_stratum , should the function stop with an error or return the whole stratum?
  • Must the sample match across machines with different locales or R versions, or only across runs on the same setup?

What a Strong Answer Covers Guidance

  • Seeding once and drawing the strata in a fixed order so the random stream is consumed the same way on every run.
  • Sampling each stratum's row positions without replacement, including strata that have a single row.
  • Computing the fractional size with floor , including sizes of zero.
  • Building the result in strata order with row names reset to 1..N .
  • What can still change the result for the same seed (locale-dependent sorting, R's sampler version).

Follow-up Questions Guidance

  • Why call set.seed() once instead of inside the per-stratum loop?
  • What happens when n_per_stratum exceeds a stratum's size?
  • set.seed() changes the caller's global random state. How would you avoid that side effect?
  • How would you write this with dplyr, and would it return the same rows for the same seed?