Home › Interview Questions › What happens when you apply a filter in Spark, and…

What happens when you apply a filter in Spark, and why does Spark wait until an action to execute the filter?

🟡 Medium Conceptual Junior level
1Times asked
Nov 2026Last seen
Nov 2026First seen

💡 Model Answer

In Spark, transformations such as filter() are lazy. When you call filter(), Spark only records the operation in the logical plan; it does not read data or perform any computation. The actual execution is deferred until an action (e.g., count(), show(), write()) is invoked. At that point, Spark builds a DAG, optimizes it, and executes the stages. This lazy evaluation allows Spark to combine multiple transformations into a single stage, apply optimizations like predicate pushdown, and avoid unnecessary data movement. Therefore, a filter is applied only when an action triggers the job, ensuring efficient execution.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500