0
0
Kafkadevops~20 mins

Stream topology in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Streams Topology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Kafka Streams topology
What is the output printed by this Kafka Streams application when it processes the input record with key "key1" and value "hello"?
Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> source = builder.stream("input-topic");
KStream<String, String> uppercased = source.mapValues(value -> value.toUpperCase());
uppercased.to("output-topic");

// Assume the input record is ("key1", "hello")
// The application prints the output record key and value after processing.
AOutput record: key=key1, value=hello
BOutput record: key=key1, value=HELLO
COutput record: key=HELLO, value=key1
DNo output record is produced
Attempts:
2 left
💡 Hint
Remember that mapValues changes only the value, not the key.
🧠 Conceptual
intermediate
1:30remaining
Understanding Kafka Streams topology nodes
In a Kafka Streams topology, which node type is responsible for consuming records from Kafka topics?
AState store node
BProcessor node
CSource node
DSink node
Attempts:
2 left
💡 Hint
Think about where the data enters the topology.
Predict Output
advanced
2:30remaining
Output of a Kafka Streams topology with branching
Given this Kafka Streams topology code, what output records are sent to the "even-topic" and "odd-topic" when the input records have values 2 and 3?
Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, Integer> source = builder.stream("numbers-topic", Consumed.with(Serdes.String(), Serdes.Integer()));

Map<String, KStream<String, Integer>> branches = source.split(Named.as("branch-"))
    .branch((key, value) -> value % 2 == 0, Branched.withConsumer(ks -> ks.to("even-topic")))
    .branch((key, value) -> value % 2 != 0, Branched.withConsumer(ks -> ks.to("odd-topic")))
    .noDefaultBranch();
Aeven-topic receives (key, 2), odd-topic receives (key, 3)
BNo records are sent to either topic
Ceven-topic receives both (key, 2) and (key, 3), odd-topic receives nothing
Deven-topic receives (key, 3), odd-topic receives (key, 2)
Attempts:
2 left
💡 Hint
Check the condition for each branch carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in Kafka Streams topology definition
What error will this Kafka Streams topology code produce when building the topology?
Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> source = builder.stream("input-topic");
source.filter((key, value) -> value.length() > 3);
source.to("output-topic");
ACompilation error due to missing semicolon
BRuntime NullPointerException
CNo records will be sent to output-topic
DThe output topic will contain all records, including those with value length <= 3
Attempts:
2 left
💡 Hint
Consider what happens to the filtered stream.
Predict Output
expert
3:00remaining
Final output of a Kafka Streams topology with stateful aggregation
What is the output printed by this Kafka Streams topology after processing the input records ("key1", "apple"), ("key1", "banana"), and ("key1", "apple") on the "fruits-topic"?
Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> source = builder.stream("fruits-topic");

KTable<String, Long> counts = source.groupByKey().count();

counts.toStream().foreach((key, count) -> System.out.println(key + ": " + count));
Akey1: 3
Bkey1: 2
Ckey1: 1
DNo output is printed
Attempts:
2 left
💡 Hint
Count counts the number of records per key, regardless of value.