0
0
Apache Sparkdata~20 mins

Map, filter, and flatMap operations in Apache Spark - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spark Map-Filter-FlatMap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of map operation on RDD
What is the output of the following Spark code snippet?
Apache Spark
rdd = sc.parallelize([1, 2, 3, 4])
result = rdd.map(lambda x: x * 2).collect()
print(result)
A[2, 4, 6, 8]
B[1, 2, 3, 4]
C[1, 4, 9, 16]
D[4, 8, 12, 16]
Attempts:
2 left
💡 Hint
Remember that map applies the function to each element individually.
Predict Output
intermediate
2:00remaining
Output of filter operation on RDD
What will be the output of this Spark code?
Apache Spark
rdd = sc.parallelize([10, 15, 20, 25, 30])
result = rdd.filter(lambda x: x > 20).collect()
print(result)
A[15, 25]
B[20, 25, 30]
C[10, 15, 20]
D[25, 30]
Attempts:
2 left
💡 Hint
Filter keeps elements where the condition is True.
data_output
advanced
2:30remaining
Result of flatMap operation on RDD
Given the following Spark code, what is the output of result.collect()?
Apache Spark
rdd = sc.parallelize(['apple,banana', 'orange', 'grape,melon'])
result = rdd.flatMap(lambda x: x.split(',')).collect()
print(result)
A['apple banana', 'orange', 'grape melon']
B['apple,banana', 'orange', 'grape,melon']
C['apple', 'banana', 'orange', 'grape', 'melon']
D['apple', 'orange', 'grape']
Attempts:
2 left
💡 Hint
flatMap splits each string by comma and flattens the result.
visualization
advanced
3:00remaining
Visualizing filter and map transformations
You have an RDD with numbers from 1 to 5. You apply filter to keep even numbers, then map to square them. Which bar chart correctly shows the final RDD values?
Apache Spark
rdd = sc.parallelize([1, 2, 3, 4, 5])
filtered = rdd.filter(lambda x: x % 2 == 0)
mapped = filtered.map(lambda x: x ** 2)
result = mapped.collect()
ABars at 1, 3, 5
BBars at 4, 16
CBars at 2, 4
DBars at 1, 4, 9, 16, 25
Attempts:
2 left
💡 Hint
Filter keeps even numbers, map squares them.
🧠 Conceptual
expert
3:00remaining
Understanding difference between map and flatMap
Which statement correctly explains the difference between map and flatMap in Spark?
Amap applies a function to each element and returns one output per input; flatMap applies a function that returns a list and flattens all lists into one RDD.
Bmap applies a function and flattens the result, flatMap applies a function without flattening.
Cmap can only be used on key-value pairs, flatMap only on lists.
Dmap and flatMap are identical in Spark and can be used interchangeably.
Attempts:
2 left
💡 Hint
Think about the output size relative to input size.