New · Cohort 3Engineering Analytics Cohort 3 goes live 25 July — only 30 seatsRegister Now
PySpark Interview Questions

PySpark Interview Questions and Answers for Data Engineers

Practice PySpark interview questions on Spark basics, DataFrames, transformations, actions, lazy evaluation, joins, partitioning, caching, UDFs, ETL, and debugging.

Published by PrepNPlaced. Last updated 2026-05-31. Preparation guidance, not a hiring guarantee.

Guide

What To Learn And How To Practice

Spark basics to explain clearly

Interviewers expect you to explain why Spark exists, how DataFrames are transformed, and what lazy evaluation means for performance and debugging.

Driver, executors, jobs, stages, and tasks at a high level
Transformations vs actions
Why Spark waits until an action to execute work

DataFrame and ETL questions

Most practical PySpark questions involve reading data, selecting columns, filtering, joining, grouping, handling nulls, and writing outputs safely.

Read JSON/CSV/parquet and infer or define schema
Join two large datasets and explain join strategy
Clean nulls and duplicate records before writing output

Lazy evaluation hint

Explain that transformations build a logical plan and actions trigger execution.

Join hint

Discuss join keys, dataset size, skew, broadcast joins, and whether duplicate keys change the result.

Performance and debugging

You do not need to be a Spark internals expert for every role, but you should understand partitions, caching, skew, shuffle, and why UDFs can be slower.

Partition by access pattern and data volume
Cache only reused expensive DataFrames
Prefer built-in functions before Python UDFs

PySpark interview questions with sample answers

The answered questions data-engineering interviews lean on most. Explain the why, not just the API call.

Execution model and shuffles
Joins and skew
Caching and partitioning

RDD vs DataFrame

DataFrames are higher-level and columnar and are optimized by Catalyst and Tungsten, so they run faster and are preferred. Use RDDs only for low-level control most jobs do not need.

Transformations vs actions (lazy evaluation)

Transformations like select, filter, and join are lazy and build a plan; actions like count, collect, show, and write trigger execution. Spark optimizes the whole plan before running it.

Narrow vs wide transformations

Narrow transforms (map, filter) keep data on the same partition, while wide ones (groupBy, join, distinct) shuffle data across the network. Shuffles are the main cost to watch.

What is a broadcast join and when to use it

When one side is small, broadcast(df) ships it to every executor so the large side is not shuffled. It is ideal for a big fact table joined to a small dimension.

Handling data skew

Skew means a few keys hold most of the rows, overloading some tasks. Mitigate by salting the hot key, broadcasting the small side, or enabling adaptive query execution (AQE) skew-join handling.

cache vs persist, and when to cache

cache() keeps a DataFrame in memory; persist() lets you choose the storage level. Cache only DataFrames reused multiple times, otherwise it wastes memory.

Why are Python UDFs slow

Python UDFs serialize data between the JVM and Python row by row. Prefer built-in Spark SQL functions or vectorized pandas UDFs, which Spark can optimize.

repartition vs coalesce

repartition(n) does a full shuffle and can increase or decrease partitions; coalesce(n) only reduces partitions without a full shuffle, which is ideal before writing fewer output files.

Question bank

19 interview questions with answers

Real questions from beginner to advanced, each with a concise model answer — practice them, then rehearse live in a mock interview.

BeginnerWhat is Apache Spark and why is it used?

Spark is a distributed, in-memory engine for large-scale data processing. It parallelizes work across a cluster and keeps data in memory between steps, making it much faster than disk-based MapReduce for big data and iterative workloads.

BeginnerWhat is the difference between an RDD, a DataFrame, and a Dataset?

RDDs are the low-level distributed collection with no schema. DataFrames add a named schema and are optimized by the Catalyst engine. Datasets (Scala/Java) add compile-time type safety. In PySpark you mostly use DataFrames for performance and simplicity.

BeginnerWhat is the difference between a transformation and an action in Spark?

Transformations (select, filter, join, groupBy) are lazy and build a plan without running. Actions (count, collect, write, show) trigger execution of that plan. Nothing computes until an action is called.

BeginnerWhat is lazy evaluation in Spark?

Spark records transformations as a logical plan and only executes when an action is called. This lets the Catalyst optimizer combine and reorder operations for efficiency, such as pushing filters down before joins.

IntermediateWhat is the difference between narrow and wide transformations?

Narrow transformations (map, filter) need data only from one partition and require no data movement. Wide transformations (groupBy, join, distinct) require a shuffle that moves data across partitions and is the main performance cost.

IntermediateWhat is a shuffle in Spark and why is it expensive?

A shuffle redistributes data across partitions and executors for operations like joins and aggregations. It's expensive because it writes to disk and moves data over the network, so minimizing shuffles is key to performance.

IntermediateWhat is the difference between repartition and coalesce?

repartition(n) reshuffles data into a new number of partitions and can balance skew. coalesce(n) only reduces partitions and avoids a full shuffle by merging existing ones, so it's cheaper for shrinking partition count after a filter.

IntermediateWhat is a broadcast join and when should you use it?

A broadcast join sends a small table to every executor so the large table can be joined locally without shuffling it. Use it when one side is small enough to fit in memory; it avoids the costly shuffle of a standard sort-merge join.

IntermediateWhat is the difference between cache and persist?

Both store a DataFrame for reuse to avoid recomputation. cache() uses the default storage level (memory, spilling to disk). persist() lets you choose the level, such as memory-only or disk-only. Use them when a DataFrame is reused multiple times.

IntermediateHow do you handle data skew in Spark?

Detect skew from long-tail tasks in the Spark UI, then mitigate by salting the skewed key, broadcasting the small side, enabling Adaptive Query Execution's skew-join handling, or pre-aggregating before the join.

IntermediateWhy should you avoid Python UDFs in PySpark when possible?

Python UDFs run row by row outside the JVM, adding serialization overhead and blocking Catalyst optimizations. Prefer built-in Spark SQL functions; if a UDF is unavoidable, use Pandas (vectorized) UDFs, which are much faster.

IntermediateHow do you read and write partitioned data in PySpark?

Read with spark.read.parquet(path) and write with df.write.partitionBy('date').parquet(path). Partitioning by a commonly filtered column enables partition pruning, so queries scan only the relevant folders.

IntermediateWhich file format is preferred in Spark and why?

Columnar formats like Parquet or ORC are preferred because they support column pruning, predicate pushdown, and compression, which drastically reduce I/O compared with row formats like CSV or JSON.

AdvancedWhat is the Catalyst optimizer?

Catalyst is Spark SQL's query optimizer. It transforms the logical plan through rule- and cost-based optimizations (predicate pushdown, column pruning, join reordering) into an efficient physical plan, which is why DataFrames outperform raw RDDs.

AdvancedWhat is the difference between the driver and executors in Spark?

The driver runs the main program, builds the DAG, and schedules work. Executors are worker processes that run tasks on partitions and hold cached data. Collecting too much data to the driver can cause out-of-memory errors.

AdvancedA PySpark job runs in dev but fails with OutOfMemory in production. How do you debug it?

Check the Spark UI for skew, large shuffles, and spills; review input sizes and partition counts; reduce data movement with broadcast joins or better partitioning; select only needed columns; and if needed raise executor memory or enable AQE.

AdvancedWhat is Adaptive Query Execution (AQE)?

AQE (Spark 3+) re-optimizes the plan at runtime using real statistics — coalescing shuffle partitions, switching join strategies, and handling skew automatically. Enabling it often improves performance without code changes.

AdvancedHow do you deduplicate rows and keep the latest record in PySpark?

Use a window: Window.partitionBy(key).orderBy(col('timestamp').desc()), assign row_number(), and filter where it equals 1. This keeps the most recent record per key without a costly self-join.

AdvancedWhat is the difference between groupBy().agg() and a window aggregation in PySpark?

groupBy().agg() collapses rows into one per group. A window aggregation over a Window spec computes the aggregate while keeping every original row, which is needed for running totals, rankings, and per-row comparisons.

FAQ

Common Questions

What is the most common PySpark interview topic?

DataFrames, transformations vs actions, joins, lazy evaluation, and performance basics are the most common starting points.

Do I need Scala for PySpark roles?

Not always. Many roles use Python APIs, but understanding Spark concepts matters more than memorizing only syntax.

How should I explain lazy evaluation?

Say that transformations build a plan, and Spark executes the plan only when an action such as count, collect, or write is called.

Are UDFs bad?

Not always, but built-in Spark functions are usually easier for Spark to optimize. Explain when a UDF is necessary.

How do I prepare projects for PySpark interviews?

Build an ETL-style project with raw data, transformations, joins, quality checks, partitioned output, and a README with tradeoffs.

How can PrepNPlaced help?

Use data courses and Open Learning for foundations, then AI Mock Interview to practice PySpark explanations.

Next Step

Turn The Guide Into Practice

Use PrepNPlaced tools to turn this learning path into resume proof, targeted practice, and interview-ready explanations.

Explore Data Courses