Null and duplicate detection in Apache Spark - Time & Space Complexity
We want to know how the time needed to find nulls and duplicates changes as the data grows.
How does checking for missing or repeated data scale with bigger datasets?
Analyze the time complexity of the following code snippet.
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
df = spark.read.csv('data.csv', header=True, inferSchema=True)
# Count nulls in column 'age'
null_count = df.filter(df['age'].isNull()).count()
# Count number of duplicated row groups
duplicate_count = df.groupBy(df.columns).count().filter('count > 1').count()
This code counts how many rows have null values in the 'age' column and how many unique row combinations appear more than once (i.e., number of duplicated groups).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows to check for nulls and grouping all rows to find duplicates.
- How many times: Each row is examined once for null check; grouping requires sorting or hashing all rows once.
As the number of rows grows, the time to check nulls grows linearly, and grouping for duplicates grows a bit more due to sorting or hashing.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks for nulls, small grouping effort |
| 100 | About 100 checks, grouping takes more time but still manageable |
| 1000 | About 1000 checks, grouping cost grows but still roughly proportional to n log n |
Pattern observation: Null checks grow directly with data size; duplicate detection grows a bit faster due to grouping.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of rows because grouping duplicates involves sorting or hashing all data.
[X] Wrong: "Checking for duplicates is just as fast as checking for nulls because both scan the data once."
[OK] Correct: Duplicate detection requires grouping or sorting, which adds extra steps beyond a simple scan, making it slower as data grows.
Understanding how data checks scale helps you explain your approach clearly and shows you know what happens behind the scenes when working with big data.
"What if we used approximate methods like sampling to detect duplicates? How would the time complexity change?"
Practice
Solution
Step 1: Understand null detection purpose
Null detection identifies missing or empty values in dataset columns.Step 2: Differentiate from duplicates
Duplicate detection finds repeated rows, not missing values.Final Answer:
Missing or empty values in columns -> Option CQuick Check:
Null detection = Missing values [OK]
- Confusing null detection with duplicate detection
- Thinking null means zero or empty string
- Assuming null detection counts rows
Solution
Step 1: Identify method for duplicates
The method dropDuplicates() removes duplicate rows from a DataFrame.Step 2: Confirm other methods' purposes
isNull() checks for nulls, fillna() fills nulls, count() counts rows.Final Answer:
dropDuplicates() -> Option DQuick Check:
Duplicates removed by dropDuplicates() [OK]
- Using isNull() to find duplicates
- Confusing fillna() with duplicate removal
- Trying count() to detect duplicates
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)
Solution
Step 1: Count nulls in 'id'
There is one row with id = None, so null_count = 1.Step 2: Calculate duplicates
Total rows = 5, dropDuplicates() removes one duplicate row (1, 'A'), so duplicate_count = 5 - 4 = 1.Final Answer:
1 1 -> Option BQuick Check:
Nulls = 1, Duplicates = 1 [OK]
- Counting duplicates before removing nulls
- Miscounting duplicates as 2
- Ignoring None as null
df = spark.createDataFrame([(1, 'x'), (1, 'x'), (2, 'y')], ['id', 'val']) duplicates = df.dropDuplicates().count() print(duplicates)
Solution
Step 1: Understand dropDuplicates() behavior
dropDuplicates() removes duplicate rows, so count() returns unique row count.Step 2: Identify mistake in duplicate detection
Counting duplicates requires comparing total count with unique count, not just unique count alone.Final Answer:
dropDuplicates() removes duplicates, so count shows unique rows, not duplicates -> Option AQuick Check:
dropDuplicates() = unique rows, not duplicates count [OK]
- Assuming dropDuplicates() counts duplicates
- Expecting syntax error from correct code
- Ignoring difference between total and unique counts
Solution
Step 1: Filter out rows with null emails
Use filter(df['email'].isNotNull()) to keep only rows with email values.Step 2: Remove duplicates by 'user_id'
Use dropDuplicates(['user_id']) to remove rows with repeated user_id values.Final Answer:
df.filter(df['email'].isNotNull()).dropDuplicates(['user_id']) -> Option AQuick Check:
Filter null emails then drop duplicates by user_id [OK]
- Filtering null emails after dropping duplicates
- Dropping duplicates on wrong column
- Filtering for null emails instead of non-null
Ready to prove it?
Pass the test and this pattern moves unknown
