🍪

Essential cookies keep authentication working. With your permission, we also use analytics to improve the product. Privacy Policy

DDataEngPrep.tech
Home
PricingBlog
Sign InStart Free→
Home/Questions/Spark/Big Data/What is the difference between repartition and coalesce in Apache Spark?

What is the difference between repartition and coalesce in Apache Spark?

Spark/Big Datamedium1 min readFeatured

Reviewed by Aditya Kumar · Last reviewed 2026-03-24

**Repartition(n)**: Performs a full shuffle to redistribute data across exactly `n` partitions. Can increase or decrease partition count. Uses hash partitioning by default—all rows are exchanged across the network. **Coalesce(n)**: Merges existing partitions into fewer...

🤖 Analyze Your Answer
Frequency
Low
Asked at 8 companies
Category
452
questions in Spark/Big Data
Difficulty Split
88E|81M|283H
in this category
Total Bank
1,863
across 7 categories
Part of the Apache Spark Interview Preparation Hub
Explore Apache Spark Hub→
Reported in Interview Loops AtClick to view company-specific questions
🏢 BCG Interview Hub→🏢 Citi Interview Hub→🏢 Dunnhumby Interview Hub→🏢 Fragma Data Systems Interview Hub→🏢 Mastercard Interview Hub→🏢 Puma Interview Hub→🏢 Snowflake Interview Hub→🏢 Meta Interview Hub→
Interview Pro Tip

Pro-Move: Mention partition count impact on small-files problem and cloud storage costs. Red Flag: Saying 'coalesce is always better'—wrong when you need to increase parallelism or fix skew.

Key Concepts Tested
partitionpythonspark

Why This Question Matters

This medium-level Spark/Big Data question appears frequently in data engineering interviews at companies like BCG, Citi, Dunnhumby, and 5 others. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (partition, python, spark) will help you answer variations of this question confidently.

How to Approach This

Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.

Expert Answer
198 wordsIncludes code

Repartition(n): Performs a full shuffle to redistribute data across exactly n partitions. Can increase or decrease partition count. Uses hash partitioning by default—all rows are exchanged across the network.

Coalesce(n): Merges existing partitions into fewer partitions without a full shuffle. Only decreases partition count. Data is combined locally where possible; minimal network transfer.

Why It Matters (Architectural Logic): Shuffles are the most expensive operations in Spark—they drive network I/O, serialization, and memory pressure. Choosing the wrong method can 2–3x your job runtime and cost.

Scalability Trade-offs:

  • repartition: Use when increasing parallelism (e.g., from 10 → 200 partitions before a heavy aggregation) or when data is severely skewed and you need even redistribution. Cost: full shuffle.

  • coalesce: Use when reducing partitions before writes to avoid small files (e.g., coalesce(1) or coalesce(num_output_files)). Cost: minimal; risk of skew if coalescing too aggressively.
  • Cost Implications: On a 1TB dataset, repartition(1) would shuffle ~1TB; coalesce(1) shuffles only the overflow. Prefer coalesce when decreasing partitions to control write file count and cloud storage costs.

    Example:

    # Increase parallelism before expensive wide transform (full shuffle)
    df_repartitioned = df.repartition(200)
    # Reduce before write to avoid small-files problem (minimal shuffle)
    df_coalesced = df.coalesce(10)

    ⚡
    Pro Tip

    Pro-Move: Mention partition count impact on small-files problem and cloud storage costs. Red Flag: Saying 'coalesce is always better'—wrong when you need to increase parallelism or fix skew.

    Get The Complete 2026 FDE, AI & DE Master Vault (All 9 PDF Volumes)
    FDE Interview Guide, FastMCP & RAG Playbook, and 7-Volume DE Vault with 1,863+ answers →
    ⭐ Save 45%
    Related Study Guides
    ⚡

    Apache Spark Interview Questions: Beginner to Advanced

    A comprehensive guide to Spark interview questions covering RDDs, DataFrames, partitioning, shuffle optimization, and real-world performance tuning.

    22 min read →
    🏗️

    System Design for Data Engineers: Complete Prep Guide

    Learn how to approach system design interviews for data engineering roles — from pipeline architecture to streaming systems and data modeling.

    20 min read →
    📦

    Amazon Data Engineer Interview Questions 2026 — Complete Guide

    Everything you need to know about the Amazon data engineering interview loop: process, questions, and preparation strategy.

    15 min read →
    🧱

    Databricks Interview Questions: SQL, Spark & More

    Prepare for Databricks data engineer interviews with real questions about Delta Lake, Unity Catalog, Spark internals, and pipeline architecture.

    16 min read →
    🐍

    Python for Data Engineering: Interview Questions & Answers

    Essential Python interview questions for data engineers covering PySpark, pandas, file handling, API design, and ETL scripting patterns.

    17 min read →
    ⚡

    Fragma Data Systems Data Engineer Interview Questions & Answers (2026)

    Practice the 65 most asked data engineering questions at Fragma Data Systems. Covers Spark/Big Data, Behavioral, Python/Coding and more.

    13 min read →
    ⚡

    Dunnhumby Data Engineer Interview Questions & Answers (2026)

    Practice the 48 most asked data engineering questions at Dunnhumby. Covers Spark/Big Data, Python/Coding, General/Other and more.

    9 min read →
    ⚡

    Citi Data Engineer Interview Questions & Answers (2026)

    Practice the 39 most asked data engineering questions at Citi. Covers Spark/Big Data, SQL, General/Other and more.

    8 min read →
    📘

    BCG Data Engineer Interview Questions & Answers (2026)

    Practice the 36 most asked data engineering questions at BCG. Covers Spark/Big Data, SQL, Cloud/Tools and more.

    8 min read →
    ⚡

    Spark Performance Tuning: 15 Interview Questions That Separate Senior Engineers from Juniors (2026)

    Senior Spark interviews at Amazon, Databricks, and Meta focus on performance tuning, not API syntax. Master these 15 questions to prove you've run Spark at scale.

    20 min read →

    Related Spark/Big Data Questions

    hardWhat is the difference between SparkSession and SparkContext in Spark?FreemediumWhat is the difference between cache() and persist() in Spark? When would you use each?FreemediumWhat is the difference between groupByKey and reduceByKey in Spark?FreemediumWhat is the difference between narrow and wide transformations in Apache Spark? Explain with examples.FreemediumWhat strategies can you use to handle skewed data in Spark?Free
    Weak vs Strong Answer Breakdown

    Compare your response with a stronger, structured example and identify missing concepts.

    Read Answer Analysis

    Level up your prep

    Recommended
    Educative
    Educative Unlimited

    800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

    Start learning →

    Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.

    According to DataEngPrep.tech, this is one of the most frequently asked Spark/Big Data interview questions, reported at 8 companies. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

    ← Back to all questionsMore Spark/Big Data questions →
    Topic Hubs
    All Topic Hubs (12 Competencies) →All 1,863+ QuestionsSQL Interview QuestionsApache Spark QuestionsPySpark QuestionsPython for Data EngineersSystem Design InterviewData Modeling & WarehousingStreaming (Kafka/Flink)SQL Window FunctionsETL Pipeline Design
    Company Hubs
    All Company Hubs (100+ Companies) →AmazonGoogleMetaDatabricksSnowflakeNetflixUberStripeMicrosoftAppleGoldman SachsAWS Engineering
    Academies & Guides
    All Guides & ArticlesData Engineering AcademyForward Deployed AI AcademyClient Simulation StudioApache Airflow QuestionsAzure Data EngineerBehavioral & STAR MethodCloud & Distributed Tools
    AI Tools & Products
    Answer Analyzer (Free)AI Mock InterviewsSQL PlaygroundATS Resume AnalyzerOffline PDF Answer VaultsMembership & Pricing
    Company
    About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
    © 2026 DataEngPrep.tech. All rights reserved.
    AboutBlogContactDisclaimer