Top 10 R scenario questions
Try each one yourself first, then open the model answer.
1. A loop over a million rows is very slow. How do you speed it up?
Show answerHide answer
Use vectorised operations, or data.table or dplyr grouped operations. Avoid growing objects inside the loop.
2. The mean of a column returns NA. Why?
Show answerHide answer
There are missing values. Decide how to treat them, then use na.rm = TRUE if excluding them is right.
3. Numbers read from a file behave like categories. What happened?
Show answerHide answer
The column was read as text or a factor. Clean it and convert with as.numeric after as.character for factors.
4. Your analysis gives different results each run. How do you make it reproducible?
Show answerHide answer
Set a random seed, record package versions with renv, and keep the whole process in scripts.
5. A join unexpectedly multiplies rows. What do you check?
Show answerHide answer
Duplicate keys in one or both tables. Verify uniqueness before joining.
6. R runs out of memory loading a huge CSV. What are your options?
Show answerHide answer
Read only needed columns, process in chunks, use data.table or Arrow, or query the data in a database.
7. Two numbers that look equal fail an == test. Why?
Show answerHide answer
Floating-point precision. Compare with a tolerance, for example all.equal.
8. Your model fits the training data very well but predicts badly. What do you do?
Show answerHide answer
It is overfitting. Use cross-validation, simplify or regularise the model and check for data leakage.
9. A p-value is significant after you tried many variables. Can you report it?
Show answerHide answer
Not without correction. Multiple testing increases false positives, so adjust the p-values and report what was tested.
10. A chart misleads because the axis is truncated. How would you handle it?
Show answerHide answer
Start bar charts at zero, label axes and units clearly, and state any transformation.
