Challenge - 5 Problems
Spark Map-Filter-FlatMap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that map applies the function to each element individually.
✗ Incorrect
The map operation multiplies each element by 2, so the output list is each original element doubled.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Filter keeps elements where the condition is True.
✗ Incorrect
Only elements greater than 20 are kept, which are 25 and 30.
❓ data_output
advanced2: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)
Attempts:
2 left
💡 Hint
flatMap splits each string by comma and flattens the result.
✗ Incorrect
flatMap applies split(',') to each element and flattens the lists into one list of fruits.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Filter keeps even numbers, map squares them.
✗ Incorrect
Only even numbers 2 and 4 remain after filter, their squares are 4 and 16.
🧠 Conceptual
expert3:00remaining
Understanding difference between map and flatMap
Which statement correctly explains the difference between map and flatMap in Spark?
Attempts:
2 left
💡 Hint
Think about the output size relative to input size.
✗ Incorrect
map returns one output per input element; flatMap returns zero or more outputs per input and flattens them.