Interview conceptCoding & Algorithms

Python Data Manipulation And Core Coding

Asked of: Data Scientist

Last updated

Horizontal editorial infographic pipeline showing stages for Python data manipulation: raw transactions → cleaning/validation → type casting & datetime → feature engineering → aggregations (variance/Welford) → joins (merge checks) → window/ranking → sequence transforms → cleaned features & metrics.

What's being tested

Interviewers are probing Python data manipulation fluency for DS workflows: cleaning transaction-like records, computing statistics, joining/ranking data, and transforming text or sequences. You need to show correct logic, edge-case handling, and clear complexity reasoning using plain Python, pandas, and SQL-equivalent patterns.

Patterns & templates

  • Variance computation — population: (xxˉ)2/n\sum(x-\bar{x})^2/n; sample: (xxˉ)2/(n1)\sum(x-\bar{x})^2/(n-1); handle empty and single-value lists explicitly.

  • Numerically stable aggregation — prefer two-pass variance for clarity; mention Welford’s algorithm for streaming O(n) time, O(1) space.

  • Transaction cleaning with pandas — use dropna, astype, to_datetime, sort_values, groupby, agg, diff; validate duplicate IDs and negative amounts.

  • User-level time featuresdf.sort_values(["user_id", "timestamp"]), then groupby("user_id")["timestamp"].diff() for inter-event intervals.

  • Join semantics — know merge(..., how="inner|left|right|outer|cross"); always check row-count changes and duplicate keys after joins.

  • Window-function equivalents — SQL ROW_NUMBER() OVER (PARTITION BY user ORDER BY ts) maps to sort_values plus groupby().cumcount() in Python.

  • Sequence transformations — generate bigrams with zip(tokens, tokens[1:]) or list comprehension; time O(n), space O(n) unless using a generator.

Common pitfalls

Pitfall: Confusing sample variance and population variance; always ask whether the list is the full population or an observed sample.

Pitfall: Treating joins as harmless; many-to-many joins can silently inflate transaction counts, revenue, fraud labels, or conversion metrics.

Pitfall: Writing clever one-liners without explaining nulls, ties, type casting, sorting assumptions, or empty-input behavior.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Featured in interview prep guides

Practice questions

Related concepts