SQL, Pandas, And dplyr Data Manipulation
Asked of: Data Scientist
Last updated

What's being tested
These problems test vectorized conditional logic and robust dtype-aware feature engineering in pandas, plus SQL skills for aggregation, deduplication, and window-based analytics. Interviewers probe whether you can write correct, efficient transformations (no row-wise loops), handle NULL/NaN semantics, and reason about precedence and deduplication in event data.
Patterns & templates
-
Use
np.selectfor multi-condition column creation with strict precedence; fallback handled by the default array,O(n)time. -
For simple two-way logic prefer
np.where(cond, a, b)orSeries.where/.maskto preserve dtypes and NaN semantics. -
Convert and validate dates with
pd.to_datetime(..., errors='coerce')then use.dtaccessors; cast floats carefully withastype(float). -
Deduplicate events with
ROW_NUMBER() OVER (PARTITION BY key ORDER BY ts DESC)then filterrow_number = 1; ties need deterministic tie-breaker. -
Conditional counts in SQL: use
SUM(CASE WHEN cond THEN 1 ELSE 0 END)orCOUNTIF(cond)where supported for clarity. -
Month-over-month use
LAG(value) OVER (PARTITION BY country ORDER BY month)then compute(value - prev)/prev; guard divide-by-zero. -
String-id composition:
COALESCE(user_id, '') || '-' || COALESCE(email, '')orCONCAT_WS('-', ...)and treat NULLs explicitly.
Common pitfalls
Pitfall: Using chained
np.wherefor many conditions accidentally flips precedence; prefernp.selectfor clarity and correctness.
Pitfall: Casting to
intbefore filling NaNs loses null semantics; fill or use nullable dtypes (Int64) instead.
Pitfall: Not deduplicating repeated user actions inflates counts—always show the dedupe rule and tie-breaker you applied.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Analyze video flags and reviews with SQLGoogle · Data Scientist · Online Assessment · medium
- Write SQL/Python for messy event dataGoogle · Data Scientist · Technical Screen · medium
- Add a conditional column in PythonGoogle · Data Scientist · Technical Screen · medium
- Find most co‑purchased product pairs in SQLGoogle · Data Scientist · Technical Screen · medium
- Compute monthly CRR with merges and gapsGoogle · Data Scientist · Technical Screen · medium
- Deduplicate events and rank products with SQLGoogle · Data Scientist · Technical Screen · medium
- Implement R dplyr simulation and left joinGoogle · Data Scientist · Technical Screen · medium
- Compute violation rate and flag precision in SQLGoogle · Data Scientist · Onsite · medium
- Calculate Top Countries' Gmail Usage and MoM ChangeGoogle · Data Scientist · Onsite · medium
- Calculate User Deviation from Team Average MessagesGoogle · Data Scientist · Technical Screen · medium
- Analyze User Flags and Review Outcomes for Moderation PrioritizationGoogle · Data Scientist · Technical Screen · medium
- Sample and Simulate Price Adjustments in R with dplyrGoogle · Data Scientist · Technical Screen · medium
Related concepts
- SQL And Python Data ManipulationData Manipulation (SQL/Python)
- Python/Pandas Data ManipulationData Manipulation (SQL/Python)
- Python, Pandas, NumPy, And R Data ManipulationData Manipulation (SQL/Python)
- SQL/Python Data Manipulation And JoinsData Manipulation (SQL/Python)
- SQL/Python Joins, Aggregations, And Window FunctionsData Manipulation (SQL/Python)
- SQL Analytics And Event Data ManipulationData Manipulation (SQL/Python)