Bird
Raised Fist0
Kafkadevops~20 mins

GroupBy and aggregation in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the groupBy operation do in Kafka Streams?
easy
A. It sends data to an external database.
B. It deletes records from the stream.
C. It organizes records by a new key to prepare for aggregation.
D. It filters records based on a condition.

Solution

  1. Step 1: Understand the purpose of groupBy

    The groupBy operation changes the key of records to group them logically.
  2. Step 2: Connect grouping to aggregation

    Grouping is necessary before aggregation to compute summaries per group.
  3. Final Answer:

    It organizes records by a new key to prepare for aggregation. -> Option C
  4. Quick Check:

    groupBy = organize by new key [OK]
Hint: GroupBy changes keys to group data before aggregation [OK]
Common Mistakes:
  • Thinking groupBy deletes data
  • Confusing groupBy with filtering
  • Assuming groupBy sends data outside Kafka
2. Which of the following is the correct syntax to perform a count aggregation after grouping a Kafka stream named orders by customerId?
easy
A. orders.count().groupBy(value -> value.customerId);
B. orders.groupBy((key, value) -> value.customerId).count();
C. orders.groupByKey().sum();
D. orders.aggregate((key, value) -> value.customerId).count();

Solution

  1. Step 1: Identify correct groupBy syntax

    The groupBy method takes a key selector function like (key, value) -> value.customerId.
  2. Step 2: Apply count aggregation after grouping

    Calling count() after groupBy correctly counts records per group.
  3. Final Answer:

    orders.groupBy((key, value) -> value.customerId).count(); -> Option B
  4. Quick Check:

    groupBy then count = correct syntax [OK]
Hint: Group first, then aggregate like count() [OK]
Common Mistakes:
  • Calling count() before groupBy
  • Using groupByKey() without key selector
  • Mixing aggregate() with wrong parameters
3. Given the following Kafka Streams code snippet, what is the output type of customerCounts?
var customerCounts = orders
  .groupBy((key, order) -> order.customerId)
  .count();
medium
A. KTable<String, Long>
B. KStream<String, Integer>
C. KStream<String, Long>
D. KTable<String, String>

Solution

  1. Step 1: Understand groupBy and count return types

    Grouping returns a grouped stream, and count() returns a KTable with counts per key.
  2. Step 2: Identify key and value types

    The key is String (customerId), and count returns Long values.
  3. Final Answer:

    KTable<String, Long> -> Option A
  4. Quick Check:

    count() returns KTable with Long values [OK]
Hint: count() after groupBy returns KTable with Long values [OK]
Common Mistakes:
  • Assuming count returns KStream
  • Confusing Long with Integer type
  • Mixing up KStream and KTable
4. Identify the error in this Kafka Streams code snippet:
var result = orders
  .groupBy(order -> order.customerId)
  .count();
medium
A. Missing a filter() before groupBy.
B. count() cannot be called after groupBy.
C. The variable 'orders' must be a KTable, not a KStream.
D. groupBy requires a key-value mapper, not a single argument.

Solution

  1. Step 1: Check groupBy method signature

    groupBy expects a function with two parameters: key and value, e.g., (key, value) -> newKey.
  2. Step 2: Identify incorrect lambda usage

    The code uses a single-argument lambda order -> order.customerId, which is invalid for groupBy.
  3. Final Answer:

    groupBy requires a key-value mapper, not a single argument. -> Option D
  4. Quick Check:

    groupBy needs (key, value) -> newKey [OK]
Hint: groupBy needs two parameters in lambda [OK]
Common Mistakes:
  • Using single-argument lambda for groupBy
  • Thinking count() is invalid after groupBy
  • Assuming orders must be KTable
5. You want to compute the total sales amount per product category from a Kafka stream sales where each record has category and amount. Which Kafka Streams code snippet correctly performs this aggregation?
hard
A. sales.groupBy((key, sale) -> sale.category).aggregate(() -> 0.0, (key, newVal, agg) -> agg + newVal.amount);
B. sales.groupBy((key, sale) -> sale.category).reduce((agg, newVal) -> agg + newVal.amount);
C. sales.groupByKey().count();
D. sales.countBy((key, sale) -> sale.category);

Solution

  1. Step 1: Choose aggregation method for summing amounts

    To sum amounts, use aggregate() with initializer and adder function.
  2. Step 2: Verify groupBy key selector and aggregation logic

    Grouping by sale.category and adding newVal.amount to accumulator is correct.
  3. Final Answer:

    sales.groupBy((key, sale) -> sale.category).aggregate(() -> 0.0, (key, newVal, agg) -> agg + newVal.amount); -> Option A
  4. Quick Check:

    aggregate with initializer sums amounts per category [OK]
Hint: Use aggregate() with initializer to sum values per group [OK]
Common Mistakes:
  • Using reduce() without matching types
  • Calling count() instead of sum
  • Using non-existent countBy() method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard