Bird
Raised Fist0
Apache Sparkdata~20 mins

Understanding partitions in Apache Spark - Practice Questions & Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the number of partitions after this operation?
Given a Spark DataFrame with 4 partitions, what will be the number of partitions after applying df.repartition(6)?
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
df = spark.createDataFrame(data, ['id', 'value'])
df = df.repartition(6)
num_partitions = df.rdd.getNumPartitions()
print(num_partitions)
A4
B6
C0
D1
Attempts:
2 left
💡 Hint
Repartition changes the number of partitions to the specified number.
🧠 Conceptual
intermediate
1:30remaining
Why is partitioning important in Spark?
Which of the following best explains why partitioning data is important in Apache Spark?
AIt helps distribute data across nodes to enable parallel processing.
BIt compresses data to save storage space.
CIt encrypts data for security purposes.
DIt converts data into a different file format.
Attempts:
2 left
💡 Hint
Think about how Spark processes data across a cluster.
❓ data_output
advanced
2:00remaining
What is the output of this partition count code?
Consider this Spark code snippet. What will be printed as the number of partitions?
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data = [(i,) for i in range(10)]
df = spark.createDataFrame(data, ['num'])
df2 = df.coalesce(2)
print(df2.rdd.getNumPartitions())
A1
B10
C2
D5
Attempts:
2 left
💡 Hint
Coalesce reduces the number of partitions without full shuffle.
🔧 Debug
advanced
2:00remaining
Identify the error in this partitioning code
What error will this Spark code raise?
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data = [(1, 'x'), (2, 'y')]
df = spark.createDataFrame(data, ['id', 'val'])
df2 = df.repartition(-3)
print(df2.rdd.getNumPartitions())
AValueError: Number of partitions must be positive
BTypeError: repartition argument must be a string
CNo error, prints 3
DRuntimeError: repartition failed due to negative value
Attempts:
2 left
💡 Hint
Number of partitions cannot be negative.
🚀 Application
expert
2:30remaining
How many partitions after chained operations?
Given a DataFrame with 8 partitions, what is the number of partitions after these chained operations?
df2 = df.repartition(4).coalesce(2).repartition(5)
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data = [(i,) for i in range(20)]
df = spark.createDataFrame(data, ['val']).repartition(8)
df2 = df.repartition(4).coalesce(2).repartition(5)
print(df2.rdd.getNumPartitions())
A2
B4
C8
D5
Attempts:
2 left
💡 Hint
Repartition sets partitions exactly; coalesce reduces partitions without shuffle.

Practice

(1/5)
1. What is the main purpose of partitions in Apache Spark?
easy
A. To compress data to save space
B. To split data into smaller chunks for parallel processing
C. To encrypt data for security
D. To store data permanently on disk

Solution

  1. Step 1: Understand what partitions do in Spark

    Partitions divide data into smaller parts so Spark can process them at the same time.
  2. Step 2: Compare options with this purpose

    Only To split data into smaller chunks for parallel processing matches this purpose; others describe unrelated tasks.
  3. Final Answer:

    To split data into smaller chunks for parallel processing -> Option B
  4. Quick Check:

    Partitions = split data for parallelism [OK]
Hint: Partitions split data to run tasks in parallel [OK]
Common Mistakes:
  • Confusing partitions with data storage
  • Thinking partitions encrypt data
  • Assuming partitions compress data
2. Which of the following is the correct way to set the number of partitions when creating an RDD in Spark?
easy
A. sc.parallelize(data, 4)
B. sc.parallelize(data, numPartitions=4)
C. sc.parallelize(data).setPartitions(4)
D. sc.parallelize(data).partitions(4)

Solution

  1. Step 1: Recall the syntax of sc.parallelize

    The method sc.parallelize takes data and an optional second argument for number of partitions as a positional argument.
  2. Step 2: Check which option matches this syntax

    sc.parallelize(data, 4) uses sc.parallelize(data, 4) which is correct. sc.parallelize(data, numPartitions=4) uses a keyword argument which is invalid. Options C and D use non-existent methods.
  3. Final Answer:

    sc.parallelize(data, 4) -> Option A
  4. Quick Check:

    sc.parallelize(data, 4) sets partitions [OK]
Hint: Use second positional argument to set partitions [OK]
Common Mistakes:
  • Using keyword argument numPartitions (not supported)
  • Trying to set partitions after creation with wrong methods
  • Confusing method names for partition setting
3. Given the following Spark code, what will be the number of partitions after execution?
rdd = sc.parallelize(range(10), 3)
rdd2 = rdd.repartition(5)
print(rdd2.getNumPartitions())
medium
A. 3
B. 1
C. 10
D. 5

Solution

  1. Step 1: Understand initial partitioning

    The RDD is created with 3 partitions using sc.parallelize(range(10), 3).
  2. Step 2: Analyze repartition call

    Calling rdd.repartition(5) changes the number of partitions to 5.
  3. Final Answer:

    5 -> Option D
  4. Quick Check:

    repartition(5) sets partitions to 5 [OK]
Hint: repartition changes partitions to given number [OK]
Common Mistakes:
  • Assuming repartition keeps original partition count
  • Confusing repartition with coalesce
  • Thinking getNumPartitions returns data size
4. Identify the error in the following Spark code snippet:
rdd = sc.parallelize([1, 2, 3, 4])
rdd2 = rdd.repartition(-2)
print(rdd2.getNumPartitions())
medium
A. parallelize requires at least 2 partitions
B. repartition method does not exist
C. Negative number of partitions is invalid
D. getNumPartitions cannot be called on rdd2

Solution

  1. Step 1: Check repartition argument

    repartition requires a positive integer for number of partitions. Negative values are invalid.
  2. Step 2: Verify other method usages

    repartition exists, parallelize accepts any positive partition count, getNumPartitions works on RDDs.
  3. Final Answer:

    Negative number of partitions is invalid -> Option C
  4. Quick Check:

    Partitions must be positive [OK]
Hint: Partitions count must be positive integer [OK]
Common Mistakes:
  • Using negative or zero partitions
  • Thinking repartition method is missing
  • Assuming getNumPartitions is invalid
5. You have a large dataset with 100 partitions, but your cluster has only 10 cores. Which Spark operation is best to optimize performance by reducing the number of partitions without causing a full shuffle?
hard
A. Use coalesce(10) to reduce partitions
B. Use repartition(100) to keep partitions same
C. Use repartition(10) to reduce partitions
D. Use mapPartitions to manually split partitions

Solution

  1. Step 1: Understand cluster resources and partitions

    Having more partitions than cores can cause overhead. Reducing partitions to match cores improves efficiency.
  2. Step 2: Choose operation that reduces partitions without full shuffle

    coalesce reduces partitions efficiently without full shuffle, unlike repartition which causes shuffle.
  3. Final Answer:

    Use coalesce(10) to reduce partitions -> Option A
  4. Quick Check:

    coalesce reduces partitions without shuffle [OK]
Hint: Use coalesce to reduce partitions without shuffle [OK]
Common Mistakes:
  • Using repartition causes expensive shuffle
  • Not matching partitions to cluster cores
  • Trying manual partition splitting unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard