These Apache Spark interview questions cover Spark architecture, RDDs, DataFrames, Spark SQL, Structured Streaming, partitioning, joins, caching, performance tuning, and PySpark. The answers are written for freshers as well as experienced data engineers who need to explain how Spark behaves in practical workloads.
Use this guide with the main Spark Tutorial. For each question, prepare a short definition, explain the internal behavior, and give an example from a batch, streaming, or data-engineering pipeline.
- Apache Spark interview questions for freshers
- Intermediate Spark architecture and DataFrame questions
- Advanced Spark performance and scenario-based questions
- PySpark coding interview questions
- Apache Spark interview preparation FAQs
Apache Spark Interview Questions for Freshers
What is Apache Spark?
Apache Spark is an open-source, distributed processing engine for large-scale data workloads. It provides APIs for processing data across a cluster and includes modules for SQL, structured data processing, streaming, machine learning, and graph processing.
When should a project use Apache Spark?
Spark is appropriate when a workload must process data in parallel across multiple machines, combine large datasets, run iterative transformations, or support both batch and streaming pipelines. A smaller single-machine tool may be simpler when the data fits comfortably on one computer and distributed processing would add unnecessary operational overhead.
What are the main Apache Spark modules?
- Spark Core: Provides scheduling, memory management, fault recovery, and the RDD API.
- Spark SQL: Processes structured data through SQL, DataFrames, and Datasets.
- Structured Streaming: Processes streaming data with the structured APIs.
- MLlib: Provides distributed machine-learning algorithms, feature transformers, pipelines, and evaluation tools.
- GraphX: Provides graph-processing APIs and algorithms for Scala applications.
Why can Spark be faster than traditional MapReduce processing?
Spark can keep reusable intermediate data in memory, execute multiple transformations as a directed acyclic graph, and optimize structured queries before execution. Traditional MapReduce workflows commonly materialize intermediate results between separate jobs. Actual performance depends on the query plan, storage format, cluster resources, partitioning, serialization, and data volume, so a fixed speed multiplier should not be assumed.
Which programming languages can be used with Spark?
Spark provides primary APIs for Scala, Java, Python, and R. Spark SQL can also be used directly through SQL statements. PySpark is the Python API for Spark and is widely used in data-engineering and analytics pipelines.
Which cluster managers can run Spark applications?
Common Spark cluster managers include Spark Standalone, Hadoop YARN, and Kubernetes. A Spark application can also run locally for development and testing. Apache Mesos appears in older Spark material, but candidates should confirm whether it is relevant to the Spark version and platform used by the employer.
Which data sources can Apache Spark access?
Spark can read and write data through built-in and external connectors. Common sources include HDFS, object storage, local file systems, Apache Hive, Apache Kafka, JDBC databases, Apache Cassandra, Apache HBase, and files stored as Parquet, ORC, Avro, JSON, CSV, or text. Connector availability and compatibility depend on the Spark distribution and runtime environment.
What is the difference between an RDD, a DataFrame, and a Dataset?
| Spark abstraction | Characteristics | Typical use |
|---|---|---|
| RDD | Low-level distributed collection with explicit transformations and actions | Unstructured data, custom partition-level logic, or APIs that require RDDs |
| DataFrame | Distributed table with named columns and a schema; optimized by Spark SQL | Most ETL, analytics, aggregation, and data-engineering workloads |
| Dataset | Strongly typed structured API available in Scala and Java | JVM applications that need compile-time types with Spark SQL optimization |
PySpark uses DataFrames but does not provide the same typed Dataset API available in Scala and Java.
What is an RDD in Apache Spark?
RDD stands for Resilient Distributed Dataset. It is an immutable collection divided into partitions that Spark can process in parallel. It is resilient because Spark records the lineage of transformations and can recompute a lost partition when a failure occurs.
What are Spark transformations and actions?
A transformation describes a new distributed dataset derived from an existing one. Examples include map, filter, select, join, and groupBy. An action requests a result or writes data, causing Spark to execute the required transformations. Examples include count, collect, take, and write operations.
What does lazy evaluation mean in Spark?
Spark does not immediately execute most transformations. It records them as a logical plan or lineage and waits for an action. This allows Spark to optimize the work, combine compatible operations, and avoid processing data that the final result does not require.
What is SparkContext?
SparkContext represents the connection between a driver program and the Spark execution environment. It coordinates access to cluster resources and supports lower-level operations such as creating RDDs and broadcast variables. Modern applications generally start with SparkSession, which provides access to SparkContext and the structured APIs.
What is SparkSession?
SparkSession is the main entry point for DataFrame and Spark SQL operations. It combines capabilities that older applications accessed through separate SQLContext and HiveContext objects. In PySpark, the active session is commonly created with SparkSession.builder.
What is Spark MLlib?
MLlib is Spark’s machine-learning library. Its DataFrame-based APIs include algorithms and utilities for classification, regression, clustering, recommendation, feature engineering, model evaluation, tuning, and machine-learning pipelines.
What is the difference between cache() and persist() in Spark?
Both methods mark a dataset for reuse after it is computed. cache() applies the default storage level for the relevant Spark abstraction, while persist() allows the application to select an available storage level. The exact default can differ between RDDs and structured APIs and can also vary by Spark version, so it should be verified in the runtime documentation.
When should data be cached in Spark?
Cache a DataFrame or RDD when the same expensive result is reused by multiple actions or later stages. Caching one-use data consumes memory without providing a benefit. After the reusable work is complete, call unpersist() so that executors can release the cached blocks.
What is a sliding window operation in Spark streaming?
A sliding window groups records that arrive within a specified time interval and recalculates results as the window advances. For example, a pipeline may compute the number of events observed during the last ten minutes and update the result every minute. In Structured Streaming, event-time windows are often combined with watermarks to manage late data and bound retained state.
Intermediate Spark Architecture and DataFrame Interview Questions
What are the driver, executors, jobs, stages, and tasks in Spark?
- Driver: Runs the application logic, creates the Spark session, builds execution plans, and coordinates work.
- Executor: A process on a worker node that runs tasks and stores cached or shuffle data for an application.
- Job: Work initiated by an action.
- Stage: A group of tasks that can run without crossing a shuffle boundary.
- Task: The smallest scheduled unit of work, normally operating on one partition.
How does a Spark application select its master?
The master can be supplied by the deployment command, runtime configuration, or application configuration. Production applications normally receive environment-specific settings from spark-submit or the managed platform instead of embedding the cluster address in application code. The following existing example explicitly creates a local master:
SparkConf conf = new SparkConf().setAppName("JavaKMeansExample")
.setMaster("local[2]")
.set("spark.executor.memory","3g")
.set("spark.driver.memory", "3g");
JavaSparkContext jsc = new JavaSparkContext(conf);
Setting local[2] uses two local worker threads and is suitable for development or tests. Cluster deployments should provide the appropriate master and deployment settings externally.
How can a Spark application be configured?
Spark properties can be supplied through SparkConf, command-line options passed to spark-submit, a Spark properties file, or platform-specific configuration. Deployment properties such as executor memory and cores are generally controlled outside the application so the same code can run in different environments.
- Application name and master URL
- Driver and executor memory
- Driver and executor cores
- Number of executor instances or dynamic allocation settings
- Serializer and compression settings
- Shuffle partition count
- Local storage directories
- Event logging and application monitoring
- Network, timeout, and retry settings
Reference: Configure Spark Application
What is the purpose of spark-env.sh?
The spark-env.sh file sets environment variables used by Spark processes in installations where administrators manage Spark directly. It can contain Java, Python, memory, host, or daemon-related settings. Cluster services and managed platforms may provide these values through their own configuration systems instead.
Reference: Configure Spark Ecosystem
What is a narrow transformation versus a wide transformation?
A narrow transformation can obtain each output partition from a limited number of input partitions, often without moving data across executors. Examples include many map and filter operations. A wide transformation requires data from multiple input partitions and normally causes a shuffle. Examples include groupByKey, distinct, repartitioning, and many joins.
What is a shuffle in Apache Spark?
A shuffle redistributes records among partitions, usually according to a key or partitioning rule. It can involve serialization, network transfer, sorting, and disk I/O. Shuffles are required for many useful operations, but unnecessary or badly balanced shuffles can become a major performance bottleneck.
How can data transfers and unnecessary shuffles be reduced?
- Filter rows and select required columns before expensive joins or aggregations.
- Use a broadcast join when one side is small enough for executor memory.
- Avoid repeated repartitioning and unnecessary global sorting.
- Use aggregation methods that perform partial aggregation before shuffle.
- Preserve useful partitioning when several operations use the same keys.
- Inspect the physical plan and Spark UI rather than assuming every shuffle can be removed.
What is the difference between repartition() and coalesce()?
repartition() reshuffles data and can increase or decrease the number of partitions while improving distribution. coalesce() is commonly used to reduce partitions with less movement, but the resulting partitions may be uneven. Repartitioning is usually preferred when balanced output or increased parallelism is required.
What is the difference between groupByKey() and reduceByKey()?
For pair RDD aggregations, groupByKey() sends all values for each key across the shuffle and groups them. reduceByKey() can combine values within each partition before transferring data, which commonly reduces network and memory pressure. Use groupByKey() only when the complete collection of values is genuinely required.
What are broadcast variables and accumulators?
A broadcast variable distributes a read-only value efficiently to executors so that it does not need to be sent repeatedly with tasks. An accumulator supports associative additions from tasks and is commonly used for counters or diagnostics. Business results should not depend on accumulator updates because task retries can complicate update behavior.
What is a broadcast join?
In a broadcast join, Spark sends the smaller relation to executors and joins it locally with partitions of the larger relation. This can avoid shuffling the large dataset. It is appropriate only when the broadcast side fits safely in executor memory; otherwise it may cause memory pressure or failures.
How does Spark recover from executor or partition failure?
Spark can retry failed tasks and recompute lost partitions from lineage. Persisted data may also be replicated when a storage level requests replication. For streaming queries, checkpoint and write-ahead information help restore progress and state, subject to the guarantees of the source and sink.
Does Spark need to be installed manually on every YARN or Kubernetes worker?
Not necessarily. On YARN, Spark dependencies can be distributed with the application or made available by the cluster configuration. On Kubernetes, Spark commonly runs in container images that contain the required runtime. The correct setup depends on the cluster manager and deployment method.
How did Spark integrate with Apache Mesos?
Historically, the Spark driver connected to a Mesos master and executors used Spark binaries available to the cluster. Older deployments could configure locations such as spark.mesos.executor.home. Because Mesos relates mainly to older Spark environments, candidates should discuss it only when it appears in the target organization’s stack.
What is the purpose of GraphX in Spark?
GraphX is Spark’s Scala API for graph-parallel computation. It represents graphs through vertices and edges and provides operators and algorithms such as PageRank and connected components. It is relevant when relationships between entities are central to the analysis.
How does Spark SQL optimize DataFrame queries?
Spark SQL analyzes the logical query, applies optimization rules, selects a physical execution plan, and generates executable code where supported. Predicate pushdown, column pruning, constant folding, join selection, and adaptive query execution can reduce the amount of work. Use explain() to inspect the selected plan.
Advanced Spark Performance and Scenario-Based Interview Questions
A Spark job is slow. How would you diagnose it?
Start with evidence from the Spark UI and execution plan. Identify the slow stage, compare task durations, inspect shuffle read and write sizes, check spill and garbage collection, review executor failures, and look for uneven partition sizes. Then determine whether the cause is skew, excessive shuffling, insufficient parallelism, small files, an inefficient join, a Python UDF, repeated computation, or unsuitable resource settings.
How do you identify and handle data skew in Spark?
Data skew occurs when a few partitions contain much more data or work than others. In the Spark UI, skew often appears as a small number of tasks running far longer or reading far more shuffle data than their peers. Possible remedies include filtering abnormal keys, broadcasting a small table, salting heavily repeated keys, pre-aggregating data, changing the partitioning strategy, or using adaptive skew-join handling where supported.
What causes out-of-memory errors in Spark executors?
- Partitions that are too large for executor memory
- Broadcasting a table that is not actually small
- Collecting or materializing large objects in memory
- Heavy aggregation or sorting with insufficient execution memory
- Excessive caching or failure to unpersist unused datasets
- Memory-intensive user-defined functions or object representations
- Data skew that sends a disproportionate amount of data to one task
Increasing memory may hide the symptom without correcting the design. Check partition sizes, execution plans, spills, cached blocks, and skew before changing cluster resources.
Why is collect() dangerous on a large DataFrame?
collect() transfers every result row to the driver. If the result is large, the driver can run out of memory or spend excessive time transferring and deserializing data. Use distributed writes, aggregations, limit(), take(), or a small sampled result when full collection is unnecessary.
How would you optimize a join between a very large fact table and a small lookup table?
Filter both tables and project only required columns first. If the lookup table fits safely in executor memory, use or permit a broadcast join. Confirm the physical plan, validate that join keys have compatible types, and check for duplicated or skewed lookup keys. If broadcasting is unsafe, repartitioning both sides by the join key may provide a more suitable shuffle join.
What is Adaptive Query Execution in Spark SQL?
Adaptive Query Execution can modify parts of a query plan using runtime statistics. Depending on the Spark version and settings, it can coalesce small shuffle partitions, change a join strategy, or mitigate some skewed joins. It improves many workloads, but it does not replace good partitioning, accurate filters, or inspection of the final physical plan.
What is the small-files problem in Spark?
A dataset containing many tiny files creates metadata, file-opening, scheduling, and listing overhead. It can also create inefficient downstream reads. Control output partition counts, compact files according to the storage system and table format, and avoid blindly writing one file per upstream partition. Writing a single file can create a different bottleneck, so target a reasonable file size instead.
When should checkpointing be used instead of caching?
Caching retains computed data for reuse but preserves its lineage. Checkpointing writes data to reliable storage and truncates the lineage, which can help with very long dependency chains and stateful streaming recovery. Checkpointing has additional I/O cost and serves a different purpose from performance-oriented caching.
How do event time, watermarks, and output modes work in Structured Streaming?
Event time is the time attached to the source event rather than the time Spark processes it. A watermark defines how late data may arrive before Spark can finalize old state for applicable operations. Output modes determine which result rows are emitted: append, update, or complete, subject to query support. A watermark is not a guarantee that every late event will be discarded at an exact boundary; behavior depends on the query and trigger progress.
Does Structured Streaming provide exactly-once processing?
The effective guarantee depends on the source, sink, query, checkpointing, and failure behavior. Spark can track progress and provide end-to-end exactly-once semantics with compatible replayable sources and idempotent or transactional sinks. A sink that creates duplicate side effects can weaken that guarantee, so candidates should describe the entire pipeline rather than claim exactly-once behavior for every configuration.
Why can a Python UDF be slower than built-in Spark functions?
Built-in DataFrame functions are visible to Spark SQL’s optimizer and execute in Spark’s optimized engine. A standard Python UDF can introduce serialization and communication between JVM and Python processes while hiding its internal logic from query optimization. Prefer built-in expressions when possible. Vectorized pandas UDFs can improve some Python workloads, but they still require careful type, memory, and batch-size management.
How would you design an idempotent Spark ETL pipeline?
An idempotent pipeline can safely process the same input again without producing incorrect duplicates. Common techniques include deterministic business keys, deduplication by a defined ordering rule, transactional merge operations, immutable input partitions, checkpointed offsets, and atomic publication of completed output. The design should also record which input version and transformation version produced each result.
PySpark Coding Interview Questions
How do you remove duplicate rows while keeping the latest record in PySpark?
Use a window partitioned by the business key and ordered by the update timestamp. Assign a row number and retain the first row in each group.
from pyspark.sql import functions as F
from pyspark.sql.window import Window
latest_first = Window.partitionBy("customer_id").orderBy(
F.col("updated_at").desc(),
F.col("record_id").desc()
)
latest_customers = (
customers
.withColumn("row_number", F.row_number().over(latest_first))
.filter(F.col("row_number") == 1)
.drop("row_number")
)
The secondary ordering column makes the result deterministic when two records have the same timestamp.
How do you calculate a running total in PySpark?
from pyspark.sql import functions as F
from pyspark.sql.window import Window
running_window = (
Window
.partitionBy("account_id")
.orderBy("transaction_time", "transaction_id")
.rowsBetween(Window.unboundedPreceding, Window.currentRow)
)
result = transactions.withColumn(
"running_amount",
F.sum("amount").over(running_window)
)
The partition resets the total for each account, while the ordering defines the sequence of transactions.
How do you perform a left anti join in PySpark?
A left anti join returns rows from the left DataFrame that do not have a matching key in the right DataFrame.
new_orders = incoming_orders.join(
processed_orders.select("order_id").distinct(),
on="order_id",
how="left_anti"
)
This pattern is useful for identifying unprocessed records, but the key definition and null-handling requirements must be agreed before using it for incremental processing.
How do you inspect the physical plan of a PySpark DataFrame?
result.explain(mode="formatted")
Inspect scans, filters, exchanges, join strategies, sorts, and adaptive-plan details. The plan should be considered together with runtime metrics from the Spark UI.
Apache Spark Interview Answer Review Checklist
- Verify that answers distinguish SparkSession, SparkContext, driver, executor, job, stage, task, and partition correctly.
- Do not repeat fixed performance multipliers without a reproducible benchmark and workload details.
- State the Spark version when discussing default storage levels, cluster-manager support, adaptive execution, or configuration defaults.
- Confirm that RDD, DataFrame, Dataset, Structured Streaming, and legacy streaming terminology is not mixed incorrectly.
- Explain which operations trigger shuffles and why, instead of describing every shuffle as avoidable.
- For tuning answers, mention the Spark UI, physical plan, task metrics, partition sizes, skew, spill, and garbage collection.
- Check that broadcast-join recommendations include memory limits and table-size considerations.
- Ensure PySpark examples use built-in functions before suggesting Python UDFs.
- For streaming guarantees, evaluate the source, sink, checkpoint, retry behavior, watermark, and business-side effects.
- Use scenario answers that state assumptions, measurement steps, expected evidence, and trade-offs.
Apache Spark Interview Preparation FAQs
Which Apache Spark topics should freshers prepare first?
Freshers should begin with Spark architecture, driver and executors, partitions, RDDs, DataFrames, transformations, actions, lazy evaluation, jobs, stages, tasks, joins, caching, and basic Spark SQL. They should also be able to explain one small ETL pipeline from reading data through writing the result.
What Spark interview questions are common for experienced data engineers?
Experienced candidates are commonly evaluated through scenarios involving slow jobs, data skew, large joins, executor memory errors, small files, partition sizing, incremental processing, Structured Streaming recovery, and production monitoring. Strong answers explain diagnosis with metrics before proposing configuration changes.
Should I prepare PySpark coding questions for a Spark interview?
Prepare PySpark when Python appears in the job description. Practice joins, aggregations, window functions, deduplication, null handling, nested data, date operations, incremental loads, query-plan inspection, and writing partitioned output. Be ready to explain both correctness and distributed execution cost.
How should I answer a scenario-based Spark interview question?
Clarify the data size, file format, cluster manager, Spark version, partition count, key distribution, service-level requirement, and failure symptoms. Then describe what you would inspect in the Spark UI and execution plan, identify likely causes, propose a measured change, and explain its trade-offs.
Is memorizing Spark configuration values enough for an interview?
No. Defaults vary by Spark version and platform, and a value that works for one workload may fail for another. Interviewers generally gain more information from how a candidate reads metrics, reasons about partitions and shuffles, tests a hypothesis, and validates the result.
TutorialKart.com