Describe R data.table syntax (DT[i, j, by], := by reference, keys) and its performance advantages on large data
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Describe the `data.table` syntax (`DT[i, j, by]`, `:=` by reference, keys) and its advantages for performance with large datasets.
```hint Where the time goes
Picture a table with tens of millions of rows. Think about what costs the most: copying columns, scanning every row to find a match, or sorting for each grouping.
```
```hint The other side of in-place updates
Modifying a table in place has a price. Think about what happens to a second variable that points to the same table.
```
### What a Strong Answer Covers
- What `i`, `j` and `by` each do, with an example that uses all three.
- What `:=` does, how it differs from the copy behavior of a base data frame, and the aliasing pitfall it creates.
- What `setkey()` does to the table and how keys enable binary-search subsetting and joins, with secondary indices as the alternative.
- Other sources of speed, such as optimized grouped aggregation, `fread`/`fwrite` and multithreading.
- The limits: readability, the cost of re-sorting, and data that does not fit in memory.
### Follow-up Questions
- How would you add a column from a lookup table to a large table without copying either one?
- When would you use `setindex()` instead of `setkey()`?
- How do `.SD` and `.SDcols` work, and how would you apply one function to many columns by group?
- What would you change when the data no longer fits in memory?
Overview: An R question that asks you to explain the data.table bracket syntax, in-place column updates and keyed tables. It tests whether you understand why these features make filtering, grouping and joining large datasets fast, and what trade-offs come with modifying data by reference.