0
0
Kafkadevops~20 mins

GroupBy and aggregation in Kafka - Practice Problems & Coding Challenges

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

Consider the following Kafka Streams code snippet that groups records by key and counts the number of records per key.

StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, Long> counts = stream
    .groupByKey()
    .count();

counts.toStream().foreach((key, value) -> System.out.println(key + ":" + value));

If the input topic contains the records with keys: a, b, a, a, b, what will be printed?

Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, Long> counts = stream
    .groupByKey()
    .count();

counts.toStream().foreach((key, value) -> System.out.println(key + ":" + value));
Aa:3\nb:2
Ba:2\nb:3
Ca:1\nb:1
Da:3\nb:3
Attempts:
2 left
💡 Hint

Count counts how many records exist per key.

Predict Output
intermediate
2:00remaining
What is the output of this Kafka Streams reduce operation?

Given the following Kafka Streams code that groups records by key and reduces values by concatenation:

StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, String> reduced = stream
    .groupByKey()
    .reduce((v1, v2) -> v1 + v2);

reduced.toStream().foreach((key, value) -> System.out.println(key + ":" + value));

If the input topic contains records with keys and values: (a, x), (b, y), (a, z), (a, w), (b, q), what will be printed?

Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, String> reduced = stream
    .groupByKey()
    .reduce((v1, v2) -> v1 + v2);

reduced.toStream().foreach((key, value) -> System.out.println(key + ":" + value));
Aa:xzw\nb:qy
Ba:xzw\nb:yq
Ca:xz\nb:yq
Da:zwx\nb:yq
Attempts:
2 left
💡 Hint

Reduce concatenates values in the order they arrive per key.

Predict Output
advanced
2:00remaining
What is the output of this Kafka Streams aggregate with initializer?

Consider this Kafka Streams aggregation that sums integer values per key:

StreamsBuilder builder = new StreamsBuilder();
KStream<String, Integer> stream = builder.stream("input-topic", Consumed.with(Serdes.String(), Serdes.Integer()));
KTable<String, Integer> aggregated = stream
    .groupByKey()
    .aggregate(
        () -> 0,
        (key, value, aggregate) -> aggregate + value
    );

aggregated.toStream().foreach((key, value) -> System.out.println(key + ":" + value));

If the input topic contains records: (a, 2), (b, 3), (a, 5), (a, -1), (b, 4), what will be printed?

Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, Integer> stream = builder.stream("input-topic", Consumed.with(Serdes.String(), Serdes.Integer()));
KTable<String, Integer> aggregated = stream
    .groupByKey()
    .aggregate(
        () -> 0,
        (key, value, aggregate) -> aggregate + value
    );

aggregated.toStream().foreach((key, value) -> System.out.println(key + ":" + value));
Aa:6\nb:7
Ba:7\nb:7
Ca:5\nb:7
Da:6\nb:4
Attempts:
2 left
💡 Hint

Aggregate sums values starting from 0 per key.

Predict Output
advanced
2:00remaining
What error does this Kafka Streams code raise?

Examine this Kafka Streams code snippet:

StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, Long> counts = stream
    .groupBy((key, value) -> value)
    .count();

What error will this code raise when compiled or run?

Kafka
StreamsBuilder builder = new StreamsBuilder();
KStream<String, String> stream = builder.stream("input-topic");
KTable<String, Long> counts = stream
    .groupBy((key, value) -> value)
    .count();
ARuntime error: NullPointerException due to missing key serializer
BNo error, code runs correctly
CCompilation error: count() requires a Materialized instance
DCompilation error: groupBy requires a KeyValueMapper but lambda returns String only
Attempts:
2 left
💡 Hint

Check the expected input type for groupBy.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting KTable after this aggregation?

Given a Kafka Streams KStream with records having keys: a, b, c, a, b, a and values: 1, 2, 3, 4, 5, 6, the following aggregation is performed:

KTable<String, Integer> result = stream
    .groupByKey()
    .aggregate(
        () -> 0,
        (key, value, aggregate) -> aggregate + value
    );

After processing all records, how many entries will the result KTable contain?

A1
B6
C3
D0
Attempts:
2 left
💡 Hint

Count distinct keys after grouping.