Why is growing a vector in a loop an anti-pattern, and how does pre-allocation help?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Overview: Asks why appending to an R vector inside a loop is slow and how pre-allocating the result changes the cost. Tests understanding of memory reallocation, the total copying cost of a loop, and idiomatic alternatives such as vapply and vectorized code.