Bird
Raised Fist0
Apache Sparkdata~20 mins

Null and duplicate detection in Apache Spark - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Null and Duplicate Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Detecting Null Values Count in Spark DataFrame
What is the output of this Spark code snippet that counts null values in each column?
Apache Spark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, sum

spark = SparkSession.builder.getOrCreate()
data = [(1, None), (2, 'a'), (None, 'b'), (4, None)]
df = spark.createDataFrame(data, ['id', 'value'])

null_counts = df.select([sum(col(c).isNull().cast('int')).alias(c) for c in df.columns])
null_counts.show()
A
+---+-----+
| id|value|
+---+-----+
|  1|    2|
+---+-----+
B
+---+-----+
| id|value|
+---+-----+
|  2|    1|
+---+-----+
C
+---+-----+
| id|value|
+---+-----+
|  1|    1|
+---+-----+
D
+---+-----+
| id|value|
+---+-----+
|  0|    2|
+---+-----+
Attempts:
2 left
💡 Hint
Remember that isNull() returns a boolean, which we cast to int to sum nulls.
❓ data_output
intermediate
1:30remaining
Number of Duplicate Rows in Spark DataFrame
Given this Spark DataFrame, how many duplicate rows does it contain?
Apache Spark
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()
data = [(1, 'a'), (2, 'b'), (1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
df = spark.createDataFrame(data, ['id', 'value'])
duplicates_count = df.count() - df.dropDuplicates().count()
print(duplicates_count)
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Duplicates are rows that appear more than once.
🔧 Debug
advanced
2:00remaining
Identify the Error in Null Filtering Code
What error does this Spark code raise when trying to filter rows with nulls in 'age' column?
Apache Spark
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()
data = [(1, 25), (2, None), (3, 30)]
df = spark.createDataFrame(data, ['id', 'age'])

filtered = df.filter(df['age'] == None)
filtered.show()
ANo error, but filtered DataFrame is empty
BSyntaxError
CTypeError
DThe filter returns no rows because '==' does not detect nulls
Attempts:
2 left
💡 Hint
In Spark, '==' does not work to check null values.
🚀 Application
advanced
2:00remaining
Remove Duplicate Rows Based on Subset of Columns
Which code snippet correctly removes duplicates based only on the 'email' column in a Spark DataFrame?
Apache Spark
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()
data = [(1, 'a@example.com'), (2, 'b@example.com'), (3, 'a@example.com'), (4, 'c@example.com')]
df = spark.createDataFrame(data, ['id', 'email'])
Adf.dropDuplicates(['id']).show()
Bdf.dropDuplicates(['email']).show()
Cdf.dropDuplicates().show()
Ddf.dropDuplicates('email').show()
Attempts:
2 left
💡 Hint
dropDuplicates expects a list of column names to consider for duplicates.
🧠 Conceptual
expert
1:30remaining
Understanding Null Handling in Spark Aggregations
When using Spark's aggregation functions like sum() on a column with null values, what is the behavior?
ANull values are treated as zero in the aggregation.
BNull values cause the aggregation to return null.
CNull values are ignored in the aggregation, sum returns the total of non-null values.
DAggregation raises an error if null values exist.
Attempts:
2 left
💡 Hint
Think about how SQL aggregates handle nulls.

Practice

(1/5)
1. What does null detection in Apache Spark help you find in a dataset?
easy
A. Columns with only numeric data
B. Rows with duplicate values
C. Missing or empty values in columns
D. The total number of rows

Solution

  1. Step 1: Understand null detection purpose

    Null detection identifies missing or empty values in dataset columns.
  2. Step 2: Differentiate from duplicates

    Duplicate detection finds repeated rows, not missing values.
  3. Final Answer:

    Missing or empty values in columns -> Option C
  4. Quick Check:

    Null detection = Missing values [OK]
Hint: Null detection finds missing data, not duplicates [OK]
Common Mistakes:
  • Confusing null detection with duplicate detection
  • Thinking null means zero or empty string
  • Assuming null detection counts rows
2. Which Apache Spark DataFrame method is used to find duplicate rows?
easy
A. count()
B. isNull()
C. fillna()
D. dropDuplicates()

Solution

  1. Step 1: Identify method for duplicates

    The method dropDuplicates() removes duplicate rows from a DataFrame.
  2. Step 2: Confirm other methods' purposes

    isNull() checks for nulls, fillna() fills nulls, count() counts rows.
  3. Final Answer:

    dropDuplicates() -> Option D
  4. Quick Check:

    Duplicates removed by dropDuplicates() [OK]
Hint: Use dropDuplicates() to find/remove duplicates [OK]
Common Mistakes:
  • Using isNull() to find duplicates
  • Confusing fillna() with duplicate removal
  • Trying count() to detect duplicates
3. Given this Spark code snippet, what is the output?
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data = [(1, 'A'), (2, None), (1, 'A'), (3, 'C'), (None, 'D')]
df = spark.createDataFrame(data, ['id', 'value'])
null_count = df.filter(df['id'].isNull()).count()
duplicate_count = df.count() - df.dropDuplicates().count()
print(null_count, duplicate_count)
medium
A. 1 2
B. 1 1
C. 0 2
D. 2 1

Solution

  1. Step 1: Count nulls in 'id'

    There is one row with id = None, so null_count = 1.
  2. Step 2: Calculate duplicates

    Total rows = 5, dropDuplicates() removes one duplicate row (1, 'A'), so duplicate_count = 5 - 4 = 1.
  3. Final Answer:

    1 1 -> Option B
  4. Quick Check:

    Nulls = 1, Duplicates = 1 [OK]
Hint: Count nulls with filter, duplicates by count difference [OK]
Common Mistakes:
  • Counting duplicates before removing nulls
  • Miscounting duplicates as 2
  • Ignoring None as null
4. What is wrong with this Spark code to detect duplicates?
df = spark.createDataFrame([(1, 'x'), (1, 'x'), (2, 'y')], ['id', 'val'])
duplicates = df.dropDuplicates().count()
print(duplicates)
medium
A. dropDuplicates() removes duplicates, so count shows unique rows, not duplicates
B. The code will raise a syntax error
C. dropDuplicates() counts duplicates instead of removing them
D. The DataFrame is empty, so count is zero

Solution

  1. Step 1: Understand dropDuplicates() behavior

    dropDuplicates() removes duplicate rows, so count() returns unique row count.
  2. Step 2: Identify mistake in duplicate detection

    Counting duplicates requires comparing total count with unique count, not just unique count alone.
  3. Final Answer:

    dropDuplicates() removes duplicates, so count shows unique rows, not duplicates -> Option A
  4. Quick Check:

    dropDuplicates() = unique rows, not duplicates count [OK]
Hint: Compare total count and dropDuplicates count to find duplicates [OK]
Common Mistakes:
  • Assuming dropDuplicates() counts duplicates
  • Expecting syntax error from correct code
  • Ignoring difference between total and unique counts
5. You have a Spark DataFrame with columns 'user_id' and 'email'. You want to remove rows where 'email' is null and also remove duplicate rows based on 'user_id'. Which code snippet correctly does this?
hard
A. df.filter(df['email'].isNotNull()).dropDuplicates(['user_id'])
B. df.dropDuplicates(['user_id']).filter(df['email'].isNull())
C. df.filter(df['email'].isNull()).dropDuplicates(['email'])
D. df.dropDuplicates().filter(df['email'].isNotNull())

Solution

  1. Step 1: Filter out rows with null emails

    Use filter(df['email'].isNotNull()) to keep only rows with email values.
  2. Step 2: Remove duplicates by 'user_id'

    Use dropDuplicates(['user_id']) to remove rows with repeated user_id values.
  3. Final Answer:

    df.filter(df['email'].isNotNull()).dropDuplicates(['user_id']) -> Option A
  4. Quick Check:

    Filter null emails then drop duplicates by user_id [OK]
Hint: Filter nulls first, then drop duplicates by key column [OK]
Common Mistakes:
  • Filtering null emails after dropping duplicates
  • Dropping duplicates on wrong column
  • Filtering for null emails instead of non-null
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