• Follow Us On :

Top 50 PySpark Interview Questions and Answers

PySpark Basics

  1. What is PySpark?
    PySpark is the Python API for Apache Spark, enabling Python developers to write Spark applications.

  2. What are the main features of PySpark?

    • Distributed computing
    • Fault tolerance
    • Lazy evaluation
    • In-memory processing
  3. What are the core components of Spark?

    • Spark Core
    • Spark SQL
    • Spark Streaming
    • MLlib
    • GraphX
  4. What is RDD in PySpark?
    Resilient Distributed Dataset (RDD) is the fundamental data structure in Spark, representing an immutable distributed collection of objects.

  5. What is the difference between RDD and DataFrame in PySpark?

    • RDD: Low-level API, no schema, supports functional programming.
    • DataFrame: High-level API, schema support, optimized query execution.

PySpark Architecture

  1. Explain the execution model of PySpark.

    • Driver: Orchestrates the execution.
    • Executor: Executes tasks on worker nodes.
    • Cluster Manager: Manages resources in the cluster.
  2. What is the role of SparkContext in PySpark?
    SparkContext is the entry point for any PySpark application, managing the connection to the Spark cluster.

  3. What is DAG in PySpark?
    Directed Acyclic Graph (DAG) represents a sequence of computations performed on data.

  4. What is a SparkSession?
    A unified entry point to work with Spark functionalities, replacing SparkContext in PySpark 2.0+.

  5. What is the role of the cluster manager in PySpark?
    It allocates resources to Spark applications. Examples include YARN, Mesos, and Standalone.

RDD Operations

  1. What are transformations in PySpark?
    Transformations create new RDDs from existing ones (e.g., map, filter).

  2. What are actions in PySpark?
    Actions trigger execution and return results (e.g., count, collect).

  3. What is lazy evaluation in PySpark?
    Transformations are not executed immediately; they are evaluated when an action is invoked.

  4. What is the difference between map() and flatMap()?

    • map(): Transforms each element into a single element.
    • flatMap(): Transforms each element into multiple elements.
  5. What is the persist() method in PySpark?
    It allows storing RDDs in memory or disk to optimize computations.

DataFrame and SQL

  1. What is a DataFrame in PySpark?
    A distributed collection of data organized into named columns.

  2. How do you create a DataFrame in PySpark?
    Using SparkSession.read for external files or SparkSession.createDataFrame for local data.

  3. What is Spark SQL?
    A module for structured data processing using SQL queries.

  4. How do you perform filtering in PySpark DataFrames?
    Using .filter() or .where() methods.

  5. What is Catalyst Optimizer in PySpark?
    An internal query optimizer that improves query execution in Spark SQL.

PySpark Programming

  1. What is a PySpark UDF?
    A user-defined function to apply custom transformations on DataFrame columns.

  2. How do you handle null values in PySpark?
    Using methods like .fillna(), .dropna(), or .replace().

  3. What is the difference between join() and union() in PySpark?

    • join(): Combines two DataFrames based on a condition.
    • union(): Combines two DataFrames vertically.
  4. What is the difference between narrow and wide transformations?

    • Narrow: Data transfer happens within a single partition (e.g., map, filter).
    • Wide: Data transfer happens between partitions (e.g., groupByKey, reduceByKey).
  5. What is the role of partitioning in PySpark?
    Partitioning distributes data across multiple nodes, optimizing parallelism and performance.

PySpark Streaming

  1. What is Spark Streaming?
    A Spark module for processing real-time data streams.

  2. How do you create a DStream in PySpark?
    Using SparkContext.streamingContext with data sources like Kafka or socket.

  3. What is a window operation in Spark Streaming?
    It applies transformations over a sliding window of data.

  4. What is the difference between batch and stream processing in PySpark?

    • Batch: Processes stored data.
    • Stream: Processes real-time data.
  5. What is the role of checkpointing in Spark Streaming?
    Checkpointing saves intermediate data for fault tolerance.

Machine Learning

  1. What is MLlib in PySpark?
    Spark’s library for machine learning, offering scalable algorithms.

  2. How do you build a pipeline in PySpark MLlib?
    Using Pipeline and PipelineStage objects.

  3. What are transformers and estimators in PySpark MLlib?

    • Transformer: Converts input data (e.g., Tokenizer).
    • Estimator: Trains a model (e.g., LinearRegression).
  4. How do you handle categorical data in PySpark?
    Using StringIndexer or OneHotEncoder.

  5. What is the role of VectorAssembler in PySpark MLlib?
    It combines multiple columns into a single vector column.

Performance Optimization

  1. What is data serialization in PySpark?
    Converting data into a byte stream for transmission or storage.

  2. How do you optimize joins in PySpark?

    • Use broadcast joins for smaller datasets.
    • Ensure proper partitioning.
  3. What is the role of caching in PySpark?
    Caching stores data in memory for reuse in subsequent actions.

  4. What is the Tungsten project in Spark?
    An initiative to improve Spark’s performance using off-heap memory and optimized code generation.

  5. How do you handle skewed data in PySpark?

    • Use salting techniques.
    • Increase parallelism with repartition().

PySpark Advanced Concepts

  1. What is a broadcast variable in PySpark?
    A read-only variable distributed to all nodes.

  2. What is an accumulator in PySpark?
    A variable used for aggregating information across tasks.

  3. What is a shared variable in PySpark?
    Variables like broadcast and accumulators used for sharing data between tasks.

  4. What is the difference between repartition() and coalesce()?

    • repartition(): Increases or decreases partitions.
    • coalesce(): Decreases partitions without shuffling.
  5. What are PySpark’s common file formats?
    CSV, JSON, Parquet, ORC, Avro.

Scenario-Based Questions

  1. How do you debug PySpark applications?

    • Use logs and Spark UI.
    • Run in local mode for debugging.
  2. How do you integrate PySpark with Kafka?
    Use structured-streaming-kafka for consuming Kafka streams.

  3. How do you process large datasets in PySpark?

    • Optimize partitions.
    • Use caching and serialization techniques.
  4. What happens if an executor fails in PySpark?
    Spark re-executes the tasks on other executors using RDD lineage.

  5. How do you deploy PySpark applications?
    Package the application and submit it using spark-submit.