Processing: Spark and PySpark
Spark is for data that does not fit in one machine's memory. PySpark DataFrame code looks almost like pandas, but a different model sits behind it: the driver builds a plan, executors run it piece by piece on different nodes, and data travels between nodes over the network. An analyst who writes such jobs or reads them in the Spark UI needs this model — otherwise they cannot explain why one line of code runs for an hour and another fails with OOM.
Almost everything in an interview comes down to three questions. When does code actually run (lazy evaluation, actions). Where does data move between machines (shuffles, join strategies, skew). What to do when a job is slow or failing (partitions, small files, memory). AQE — adaptive query execution, on by default since Spark 3.2 — removes part of the manual tuning, but not all of it. Below is a map that leads from the execution model to diagnosis.
Topic map
- Spark architecture — driver, executors, and the cluster manager, lazy evaluation, jobs, stages, and tasks.
- PySpark DataFrame — how a DataFrame differs from pandas, transformations versus actions, Python UDFs versus built-in functions and pandas UDFs.
- Shuffles and joins — narrow and wide transformations, broadcast join and its threshold, data skew, salting, and AQE.
- Spark performance —
repartitionversuscoalesce,partitionByon write, small files,cache/persist, triaging OOM and spill.
Common traps
| Mistake | Consequence |
|---|---|
Calling collect() or toPandas() on a large result | Every row lands in the single driver process, and it fails with OOM |
Assuming a filter or join line executed something | The error surfaces later, at the action, and gets looked for in the wrong place |
Not seeing the shuffle behind groupBy, distinct, join | The new stage and the disk writes stay unexplained |
| Writing logic as a Python UDF where built-in functions suffice | A Python call per row, JVM ↔ Python transfer, and lost Catalyst optimisations |
| Treating skew by adding executor memory | The hot key still lands in one task |
Putting coalesce(1) before writing a large table | The whole stage's work collapses into one task |
Interview relevance
Spark is asked about to check whether a candidate understands where the time goes in a distributed job. A strong answer rests on the mechanism — "there is a shuffle here because groupBy gathers one key's rows from different partitions" — not on a list of settings.
Typical checks:
- The roles of the driver and executors, and why
collect()crashes the driver. - Lazy evaluation: what launches a job, and how many jobs, stages, and tasks a snippet produces.
- Narrow and wide transformations, broadcast join, and the
spark.sql.autoBroadcastJoinThresholdthreshold. - Diagnosing skew from stage metrics, salting, and what AQE can do.
repartitionversuscoalesce, small files on write, and triaging an executor OOM.
Common wrong answer: "If a job is slow, give the executors more memory." In fact you first look at the task distribution in the Spark UI: if one task reads hundreds of times more than the median, it is skew, memory does not cure it — the hot key has to be spread across tasks.