Bird
Raised Fist0
Kafkadevops~20 mins

Stream topology in Kafka - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does a Kafka Streams topology represent?
easy
A. The configuration settings for Kafka brokers
B. The flow and processing steps of data in a Kafka Streams application
C. The list of Kafka topics available in the cluster
D. The schema of messages in a Kafka topic

Solution

  1. Step 1: Understand the role of topology in Kafka Streams

    A topology defines how data flows and is processed in a Kafka Streams app.
  2. Step 2: Differentiate topology from other Kafka concepts

    It is not about broker configs, topic lists, or message schemas, but about processing steps.
  3. Final Answer:

    The flow and processing steps of data in a Kafka Streams application -> Option B
  4. Quick Check:

    Topology = Data flow and processing [OK]
Hint: Topology means data flow and processing steps [OK]
Common Mistakes:
  • Confusing topology with Kafka broker settings
  • Thinking topology lists topics
  • Assuming topology defines message schema
2. Which code snippet correctly builds a Kafka Streams topology?
easy
A. StreamsBuilder builder = new StreamsBuilder(); builder.run();
B. StreamsBuilder builder = new StreamsBuilder(); builder.start();
C. StreamsBuilder builder = new StreamsBuilder(); builder.create();
D. StreamsBuilder builder = new StreamsBuilder(); builder.build();

Solution

  1. Step 1: Recall the correct method to build topology

    The method to finalize and get the topology is build().
  2. Step 2: Check each option's method

    Only StreamsBuilder builder = new StreamsBuilder(); builder.build(); uses build(), others use invalid methods.
  3. Final Answer:

    StreamsBuilder builder = new StreamsBuilder(); builder.build(); -> Option D
  4. Quick Check:

    Use build() to create topology [OK]
Hint: Use build() method on StreamsBuilder to create topology [OK]
Common Mistakes:
  • Using start(), create(), or run() instead of build()
  • Confusing topology building with starting the stream
3. Given the code snippet:
StreamsBuilder builder = new StreamsBuilder();
builder.stream("input-topic").mapValues(value -> value.toUpperCase()).to("output-topic");
Topology topology = builder.build();
System.out.println(topology.describe());

What will the printed topology description include?
medium
A. A sink node for 'input-topic' and a source node for 'output-topic'
B. Only a source node for 'input-topic' without any processing
C. A source node for 'input-topic', a processor node for mapValues, and a sink node for 'output-topic'
D. An error because mapValues is not allowed in topology

Solution

  1. Step 1: Analyze the stream processing steps

    The code reads from 'input-topic', applies mapValues to uppercase, then writes to 'output-topic'.
  2. Step 2: Understand topology description output

    The description shows source nodes (input), processor nodes (mapValues), and sink nodes (output).
  3. Final Answer:

    A source node for 'input-topic', a processor node for mapValues, and a sink node for 'output-topic' -> Option C
  4. Quick Check:

    Topology describe shows source, processor, sink [OK]
Hint: Topology describe shows source, processor, and sink nodes [OK]
Common Mistakes:
  • Thinking mapValues is not part of topology
  • Confusing source and sink nodes
  • Expecting no processor nodes in description
4. Identify the error in this Kafka Streams topology code:
StreamsBuilder builder = new StreamsBuilder();
builder.stream("input-topic").filter(value -> value.length() > 5);
Topology topology = builder.build();
System.out.println(topology.describe());
medium
A. The filtered stream is not sent to any sink topic, so no output node is created
B. The filter method is invalid and causes a syntax error
C. StreamsBuilder cannot build topology without a source topic
D. Calling build() before defining streams causes runtime error

Solution

  1. Step 1: Check the stream processing chain

    The code filters the stream but does not write the result to any output topic.
  2. Step 2: Understand topology implications

    Without a sink node, the topology has no output, which is usually an error or incomplete.
  3. Final Answer:

    The filtered stream is not sent to any sink topic, so no output node is created -> Option A
  4. Quick Check:

    Missing sink node causes incomplete topology [OK]
Hint: Always send processed stream to sink topic [OK]
Common Mistakes:
  • Assuming filter causes syntax error
  • Thinking build() requires no streams
  • Ignoring missing sink topic after processing
5. You want to create a Kafka Streams topology that reads from two topics, merges their streams, filters messages containing 'error', and writes to an output topic. Which code snippet correctly builds this topology?
hard
A. StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.merge(stream2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build();
B. StreamsBuilder builder = new StreamsBuilder(); KStream stream = builder.stream("topic1", "topic2"); stream.filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build();
C. StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.join(stream2, (v1, v2) -> v1 + v2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build();
D. StreamsBuilder builder = new StreamsBuilder(); builder.stream("topic1").filter(v -> v.contains("error")).to("output-topic"); builder.stream("topic2").filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build();

Solution

  1. Step 1: Understand the requirement

    Read from two topics, merge streams, filter messages with 'error', write to output.
  2. Step 2: Evaluate each option

    StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.merge(stream2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); merges streams correctly, filters, then writes once. StreamsBuilder builder = new StreamsBuilder(); KStream stream = builder.stream("topic1", "topic2"); stream.filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); incorrectly tries to stream two topics in one call. StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.join(stream2, (v1, v2) -> v1 + v2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); joins streams (not merge). StreamsBuilder builder = new StreamsBuilder(); builder.stream("topic1").filter(v -> v.contains("error")).to("output-topic"); builder.stream("topic2").filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); filters separately and writes twice, not merging.
  3. Final Answer:

    Option A correctly merges, filters, and writes to output topic -> Option A
  4. Quick Check:

    Merge streams, filter, then write [OK]
Hint: Merge streams before filtering and writing output [OK]
Common Mistakes:
  • Using join instead of merge for combining streams
  • Trying to stream multiple topics in one call incorrectly
  • Writing filtered streams separately without merging
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