0
0
Kafkadevops~20 mins

Filter and map operations in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Streams Filter-Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Kafka Streams: What is the output of this filter and map operation?

Consider a Kafka Streams application that processes a stream of integers. It filters out even numbers and then maps each remaining number to its square. What will be the output for the input stream [1, 2, 3, 4, 5]?

Kafka
KStream<Integer, Integer> input = builder.stream("input-topic");
KStream<Integer, Integer> processed = input
    .filter((key, value) -> value % 2 != 0)
    .mapValues(value -> value * value);
processed.to("output-topic");
A[1, 4, 9, 16, 25]
B[1, 9, 25]
C[2, 4]
D[1, 3, 5]
Attempts:
2 left
💡 Hint

Remember that filter removes even numbers, and mapValues squares the remaining numbers.

Predict Output
intermediate
2:00remaining
Kafka Streams: Result of chaining filter and map with keys

Given a Kafka Streams pipeline that filters records with keys less than 3 and maps values by adding 10, what is the output for input records with keys and values: (1, 5), (2, 7), (3, 9), (4, 11)?

Kafka
KStream<Integer, Integer> input = builder.stream("input-topic");
KStream<Integer, Integer> result = input
    .filter((key, value) -> key < 3)
    .mapValues(value -> value + 10);
result.to("output-topic");
A[(1, 15), (2, 17)]
B[(3, 19), (4, 21)]
C[(1, 5), (2, 7)]
D[(1, 15), (2, 17), (3, 19), (4, 21)]
Attempts:
2 left
💡 Hint

Filter keeps keys less than 3, then values get 10 added.

🔧 Debug
advanced
2:00remaining
Why does this Kafka Streams filter-map code cause a runtime error?

Examine the following Kafka Streams code snippet. It attempts to filter and map values but causes a runtime NullPointerException. What is the cause?

Kafka
KStream<Integer, String> input = builder.stream("input-topic");
KStream<Integer, String> result = input
    .filter((key, value) -> value.length() > 3)
    .mapValues(value -> value.toUpperCase());
result.to("output-topic");
AThe stream is missing a call to selectKey before filter, causing runtime failure.
BThe mapValues function is incorrectly used and causes a syntax error.
CThe filter predicate should use key instead of value, causing a type error.
DSome values in the stream are null, causing value.length() to throw NullPointerException.
Attempts:
2 left
💡 Hint

Think about what happens if value is null when calling value.length().

📝 Syntax
advanced
2:00remaining
Which Kafka Streams filter-map code snippet is syntactically correct?

Choose the syntactically correct Kafka Streams code that filters values greater than 10 and maps values to their double.

Ainput.filter((k, v) -> { v > 10 }).mapValues(v -> v * 2);
Binput.filter(k, v -> v > 10).mapValues(v -> v * 2);
Cinput.filter((k, v) -> v > 10).mapValues(v -> v * 2);
Dinput.filter((k, v) => v > 10).mapValues(v -> v * 2);
Attempts:
2 left
💡 Hint

Check the syntax of lambda expressions in Java.

🚀 Application
expert
2:00remaining
How many records are in the output after this Kafka Streams filter and map chain?

A Kafka Streams application reads from a topic with 6 records having values: [5, 12, 7, 20, 3, 15]. It filters values greater than 10 and then maps values to value minus 5. How many records are in the output topic?

Kafka
KStream<Integer, Integer> input = builder.stream("input-topic");
KStream<Integer, Integer> output = input
    .filter((k, v) -> v > 10)
    .mapValues(v -> v - 5);
output.to("output-topic");
A3
B4
C2
D6
Attempts:
2 left
💡 Hint

Count how many values are greater than 10 before mapping.