Skip to the page
Tecyfy

PySpark interview question · hard

How would you handle a 25GB file in Spark, and what optimizations would you apply?

The short answer25 GB is moderate for Spark: a cluster of a few executors handles it easily, if the file can be split for parallel reading.

The full answer

25 GB is moderate for Spark: a cluster of a few executors handles it easily, if the file can be split for parallel reading. So the first question is the file's format and compression; then define the schema, keep partitions around 128 MB, filter and project early, and write the result to a columnar format.

1. Can it be split?

  • Uncompressed CSV or JSON Lines, Parquet, ORC, Avro: splittable. Spark reads 128 MB pieces in parallel (spark.sql.files.maxPartitionBytes), so 25 GB becomes about 200 tasks.
  • Gzip-compressed CSV: not splittable. One task reads the whole file, slowly, while the rest of the cluster waits. Read it once, repartition immediately, and write it to Parquet or Delta; better, ask the producer for split files or a splittable compression such as bzip2, or zstd-compressed Parquet.
  • Multiline JSON or CSV with line breaks inside quoted fields (multiLine true) also can't be split.

2. Define the schema Inference on 25 GB means an extra full pass. Provide the schema:

schema = "order_id BIGINT, customer_id BIGINT, order_ts TIMESTAMP, amount DECIMAL(12,2), country STRING"
df = (spark.read
        .option("header", True)
        .option("mode", "PERMISSIVE")
        .schema(schema)
        .csv("/landing/orders_big.csv"))

3. Size the cluster sensibly About 200 input tasks fit well on, for example, 5 executors with 4–5 cores each and 16–32 GB of memory; it doesn't need to fit in memory, since Spark streams through partitions. Enable autoscaling or dynamic allocation if available.

4. Reduce early Select only needed columns and filter rows as early as possible. With columnar formats these are pushed into the scan; with CSV they still reduce shuffle and memory later.

5. Shuffle settings For joins and aggregations, set spark.sql.shuffle.partitions to roughly the data size after filtering divided by 128–200 MB, and leave adaptive query execution on to coalesce small partitions and handle skew. Broadcast small lookup tables.

6. Avoid driver bottlenecks No collect() or toPandas() of the full data; inspect with show() or aggregates.

7. Cache only if reused If several outputs come from the same cleaned data, cache it (MEMORY_AND_DISK) or write it once as an intermediate table.

8. Write efficiently

(df.write
    .mode("overwrite")
    .partitionBy("order_date")      # only if queries filter on it and partitions stay large
    .format("delta")
    .saveAsTable("silver.orders"))

Columnar output is typically several times smaller than CSV and much faster to query. Avoid producing thousands of tiny files: repartition to a sensible number before writing.

9. Make it repeatable If the file arrives regularly, ingest it incrementally (Structured Streaming's file source or Auto Loader), keep bad records in a quarantine column or table, and monitor the stage times in the Spark UI.

Last updated 16 October 2026