🎬No video for this question yet. The written answer is below.
Difference between RDD, DataFrame, and Dataset?- What are transformations vs actions in Spark?
- How do you handle data skew in Spark?
- What is partitioning and how does it improve performance?
- Difference between cache() and persist()?
📝 Answer
Apache Spark Interview Answers
Simple, clear, natural — speak it like a real interview
1
RDD vs DataFrame vs Dataset
Quick Comparison
| Feature | RDD | DataFrame | Dataset |
|---|---|---|---|
| Structure | No structure | Rows & columns | Typed rows & columns |
| Optimization | Manual only | Auto (Catalyst) | Auto (Catalyst) |
| Type Safety | No | No | Yes |
| Language | All | All (Python mostly) | Scala / Java |
| Speed | Slow | Fast | Fast |
| Real use | Rare now | Most common | Enterprise Java/Scala |
How to Say It in Interview
🎤 Speak This
In Spark, we have three ways to work with data: RDD, DataFrame, and Dataset.RDD is the basic level. It gives full control, but it does not have structure. So Spark cannot optimize it. We have to write everything manually. Because of that, it is slower and more complex. Nowadays, we rarely use RDD in real projects.
DataFrame is the most commonly used. It works like a table with rows and columns. Because it has structure, Spark can understand the data and optimize the execution. This makes it faster and easier. We can also use SQL queries on DataFrames.
Dataset is similar to DataFrame but adds type safety. This is mainly used in Scala or Java. In Python, we mostly use DataFrames only.
In my experience, in AWS projects like Glue or EMR, I always use DataFrames because they are fast, easy to write, and easy to maintain. I only use RDD if I need very special logic.
2
Transformations vs Actions
Quick Comparison
| Type | What It Does | Examples | Executes? |
|---|---|---|---|
| Transformation | Defines what to do | filter, map, groupBy, join | No (Lazy) |
| Action | Triggers execution | show, count, write, collect | Yes |
| Narrow Transform | Data stays in same partition | filter, map | No shuffle → Fast |
| Wide Transform | Data moves across network | groupBy, join | Shuffle → Slow |
How to Say It in Interview
🎤 Speak This
In Spark, there are two types of operations: transformations and actions.Transformations are like instructions. For example, filter or map. When we write them, Spark does not execute immediately. It just remembers what we want to do.
Actions are the operations that actually run the job. For example, show, count, or write. When we call an action, Spark starts execution.
This is called lazy execution. Spark waits until the final step, then runs everything together in an optimized way.
Also, there are two types of transformations — Narrow and Wide. Narrow means data stays in the same partition, so it is fast. Wide means data moves across the network, like in groupBy or join. This causes shuffle, which is slow.
So in real projects, I try to reduce wide transformations and avoid unnecessary actions. Because every action triggers full execution, which can be costly.
🔥 Key Point
Every Action triggers a full DAG execution. Avoid calling actions inside loops. Use cache() if the same DataFrame is reused across multiple actions.
3
Handling Data Skew
What is Data Skew?
- Data is not evenly distributed across partitions
- One partition gets too much data → one machine overloaded
- Other machines sit idle → overall job becomes slow
- Common in customer data, transaction logs, or categorical keys
How to Fix It
- Salting — Add a random value to the key to spread data across more partitions
- Broadcast Join — If one table is small, send it to all machines → avoids shuffle completely
- Repartition — Increase partitions, but use carefully as it can cause shuffle
- Spark UI — Check task duration — if some tasks are much longer, skew is the cause
How to Say It in Interview
🎤 Speak This
Data skew means data is not evenly distributed. For example, if one key has too much data, one partition will have more work than others. So one machine works a lot, while others are idle. This makes the job slow.First, I identify skew using Spark UI. I check if some tasks are taking much longer than others.
Then I fix it. One way is salting — I add a random value to spread the data. Another way is broadcast join. If one table is small, I send it to all machines so that shuffle is avoided.
In real projects, like customer or transaction data, skew happens often. So I always check data distribution before joins and design the logic properly.
4
Partitioning in Spark
Two Types of Partitioning
| Type | Where | How | Benefit |
|---|---|---|---|
| In-memory | Inside Spark | repartition(), coalesce() | Controls parallel tasks |
| Storage-level | S3 / HDFS | Folders like year=2025/month=04 | Spark reads only needed folders |
repartition vs coalesce
| Method | Use When | Shuffle? |
|---|---|---|
| repartition(n) | Increasing partitions | Yes — full shuffle |
| coalesce(n) | Reducing partitions (e.g. before write) | No — merge only |
How to Say It in Interview
🎤 Speak This
Partitioning means dividing data into smaller pieces so Spark can process it in parallel. Each partition is handled by one task. So more partitions means more parallel processing.There are two types. One is inside Spark, where we control partitions using repartition or coalesce. The other is in storage like S3, where data is stored in folders like year=2025 or month=04.
Partitioning improves performance because Spark reads only required data. For example, if data is partitioned by date and we query one day, Spark reads only that folder.
But we need to be careful. Too many partitions create overhead. Too few partitions reduce performance. So in real projects, I decide partitions based on data size and query pattern.
📌 Real Project Tip
In AWS Glue / EMR, always partition S3 output by date or region. Use coalesce(1) only for small files. Avoid creating too many small files — this slows down reads.
5
cache() vs persist()
Comparison
| Method | Storage | Best For | Flexible? |
|---|---|---|---|
| cache() | Memory only | Small datasets reused often | No |
| persist() | Memory + Disk (configurable) | Large datasets that can't fit in memory | Yes — storage levels |
Storage Levels for persist()
How to Say It in Interview
🎤 Speak This
Cache and persist are used to store data in memory so Spark does not recompute it again.By default, Spark recomputes data every time we use it. If the same data is used multiple times, it becomes slow.
Cache is simple. It stores data in memory only. It is good for small datasets.
Persist is more flexible. It can store data in memory and disk. This is useful when data is large and cannot fit in memory.
In real projects, I don't cache everything. I only cache data that is reused multiple times. Otherwise, it wastes memory. Also, after using cached data, I call unpersist() to free memory.
So proper use of cache and persist helps improve performance and reduce execution time.
🔥 Senior Tip
Always call df.unpersist() after you are done using the cached DataFrame. Uncleaned cache wastes executor memory and can cause OOM (Out of Memory) errors in long-running pipelines.
🎯 How to Speak in the Interview
- Speak slow and clear — don't rush
- Use simple words — avoid heavy jargon
- Always give a small real example
- Use connector phrases naturally