R copy-on-modify semantics: when copies actually happen and how they affect performance and memory

Quick Overview

Asks how R's copy-on-modify semantics work, when R actually duplicates an object, and what that costs in time and memory. Tests understanding of shared bindings, modify-in-place, data frame and list copies, and how to detect and avoid unnecessary copies in R code.

R copy-on-modify semantics: when copies actually happen and how they affect performance and memory

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain R's copy-on-modify semantics. When do copies of objects actually occur and how can this impact performance and memory? ```hint Count the names Track how many names point at an object before and after an assignment, and what R has to do when one of them changes the value. ``` ```hint Picture a loop Imagine running `tracemem()` on a vector grown with `c()` inside a loop, or on a data frame edited column by column, and ask what it would report on each iteration. ``` ### What a Strong Answer Covers - What copy-on-modify means: several bindings share one object until one of them modifies it. - How R decides whether a copy is needed, when it can modify in place, and the exception for environments. - Where copies hide in real code: function arguments, data frames, vectors grown in loops, and how lists and data frames copy shallowly. - The time and memory cost of copies, and how to detect and avoid them. ### Follow-up Questions - Why does modifying one column of a data frame cost less than modifying one row? - How do environments, R6 objects and `data.table`'s `:=` avoid copy-on-modify, and what risks does that bring? - A function receives a 2 GB data frame and changes one column before returning it. Roughly how much extra memory does the call need? - How would you confirm whether a loop copies its object on every iteration?

Overview: Asks how R's copy-on-modify semantics work, when R actually duplicates an object, and what that costs in time and memory. Tests understanding of shared bindings, modify-in-place, data frame and list copies, and how to detect and avoid unnecessary copies in R code.

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

Explain R's copy-on-modify semantics. When do copies of objects actually occur and how can this impact performance and memory?

What a Strong Answer Covers Guidance

  • What copy-on-modify means: several bindings share one object until one of them modifies it.
  • How R decides whether a copy is needed, when it can modify in place, and the exception for environments.
  • Where copies hide in real code: function arguments, data frames, vectors grown in loops, and how lists and data frames copy shallowly.
  • The time and memory cost of copies, and how to detect and avoid them.

Follow-up Questions Guidance

  • Why does modifying one column of a data frame cost less than modifying one row?
  • How do environments, R6 objects and data.table 's := avoid copy-on-modify, and what risks does that bring?
  • A function receives a 2 GB data frame and changes one column before returning it. Roughly how much extra memory does the call need?
  • How would you confirm whether a loop copies its object on every iteration?