Filter and map operations in Kafka - Time & Space Complexity
We want to understand how the time it takes to run filter and map operations changes as the data grows.
How does the number of items affect the work done by these operations?
Analyze the time complexity of the following code snippet.
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val evenNumbers = numbers.filter(n => n % 2 == 0)
val doubled = evenNumbers.map(n => n * 2)
This code filters even numbers from a list and then doubles each filtered number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list twice: once for filter, once for map.
- How many times: Each operation goes through all items in the list once.
As the list gets bigger, the work grows because each item is checked and then processed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (10 for filter + 10 for map) |
| 100 | About 200 (100 for filter + 100 for map) |
| 1000 | About 2000 (1000 for filter + 1000 for map) |
Pattern observation: The total work grows roughly twice as fast as the input size.
Time Complexity: O(n)
This means the time to run filter and map grows in a straight line with the number of items.
[X] Wrong: "Filter and map together make the time complexity O(n²) because they are two steps."
[OK] Correct: Each step goes through the list once, so the total work adds up, not multiplies. This keeps it linear, not squared.
Understanding how filter and map scale helps you explain how data processing pipelines work efficiently in real projects.
"What if we combined filter and map into one step? How would the time complexity change?"
Practice
filter operation do in Kafka Streams?Solution
Step 1: Understand filter purpose
Thefilteroperation selects messages that satisfy a given condition, removing others.Step 2: Compare with other operations
Unlikemap, which changes message content,filteronly keeps or drops messages.Final Answer:
Keeps only messages that meet a condition -> Option CQuick Check:
Filter = Keep messages by condition [OK]
- Confusing filter with map operation
- Thinking filter changes message content
- Assuming filter deletes all messages
map operation on a Kafka stream named stream to convert messages to uppercase?Solution
Step 1: Identify correct lambda syntax in Kafka Streams
Kafka Streams in Java usesmap(value -> ...)syntax for mapping values.Step 2: Check method call correctness
value.toUpperCase()correctly converts string to uppercase.Final Answer:
stream.map(value -> value.toUpperCase()) -> Option DQuick Check:
Correct lambda and method call = stream.map(value -> value.toUpperCase()) [OK]
- Using filter instead of map
- Using JavaScript arrow => instead of Java ->
- Missing parentheses in method call
stream.filter(value -> value.length() > 3)
.map(value -> value.substring(0, 3))Input messages: ["apple", "cat", "banana", "dog"]
Solution
Step 1: Apply filter condition
Keep messages with length > 3: "apple" (5), "banana" (6). "cat" and "dog" are dropped.Step 2: Apply map operation
Take first 3 characters: "apple" -> "app", "banana" -> "ban".Final Answer:
["app", "ban"] -> Option BQuick Check:
Filter then map = ["app", "ban"] [OK]
- Not filtering before mapping
- Mapping before filtering
- Including messages with length ⤠3
stream.filter(value -> value.startsWith("a"))
.map(value -> value.toUpperCase())
.filter(value -> value.length() > 5)Solution
Step 1: Check filter and map chaining
Chaining multiple filters and maps is allowed in Kafka Streams.Step 2: Verify lambda syntax and logic
Lambdas use correct syntax and logic: first filter checks start, map uppercases, second filter checks length.Final Answer:
No error; code is correct -> Option AQuick Check:
Multiple filters and maps allowed = No error; code is correct [OK]
- Thinking multiple filters are disallowed
- Confusing lambda syntax with other languages
- Assuming map changes variable type incorrectly
Solution
Step 1: Filter names starting with 'J'
Usefilter(name -> name.startsWith("J"))to keep only names starting with uppercase 'J'.Step 2: Map filtered names to lowercase
Applymap(name -> name.toLowerCase())to convert names to lowercase.Final Answer:
stream.filter(name -> name.startsWith("J")) .map(name -> name.toLowerCase()) -> Option AQuick Check:
Filter first, then map to lowercase [OK]
- Mapping before filtering causing wrong results
- Using incorrect lambda syntax
- Filtering after changing case causing misses
Ready to prove it?
Pass the test and this pattern moves unknown
