0
0
Kafkadevops~20 mins

Windowed operations in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of tumbling window aggregation
Given the following Kafka Streams code snippet that counts events per 5-second tumbling window, what is the output for the input events with timestamps in seconds: 1, 2, 3, 6, 7?
Kafka
KStream<String, String> input = builder.stream("input-topic");

KTable<Windowed<String>, Long> counts = input
  .groupByKey()
  .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofSeconds(5)))
  .count();

counts.toStream().foreach((windowedKey, count) -> {
  System.out.println("Window start: " + windowedKey.window().start() + ", count: " + count);
});
A
Window start: 0, count: 2
Window start: 5, count: 3
B
Window start: 0, count: 3
Window start: 5, count: 2
C
Window start: 1, count: 3
Window start: 6, count: 2
DWindow start: 0, count: 5
Attempts:
2 left
💡 Hint
Remember that tumbling windows are fixed-size, non-overlapping intervals starting at multiples of the window size.
Predict Output
intermediate
2:00remaining
Sliding window count output
What is the output of the following Kafka Streams sliding window count for events with timestamps 1, 2, 3, 6, 7, if the sliding window size is 5 seconds and advance interval is 1 second?
Kafka
KStream<String, String> input = builder.stream("input-topic");

KTable<Windowed<String>, Long> counts = input
  .groupByKey()
  .windowedBy(SlidingWindows.ofTimeDifferenceWithNoGrace(Duration.ofSeconds(5)))
  .count();

counts.toStream().foreach((windowedKey, count) -> {
  System.out.println("Window start: " + windowedKey.window().start() + ", count: " + count);
});
A
Window start: 1, count: 3
Window start: 2, count: 3
Window start: 3, count: 3
Window start: 6, count: 2
Window start: 7, count: 2
B
Window start: 0, count: 3
Window start: 1, count: 3
Window start: 2, count: 3
Window start: 5, count: 2
Window start: 6, count: 2
C
Window start: 1, count: 1
Window start: 2, count: 1
Window start: 3, count: 1
Window start: 6, count: 1
Window start: 7, count: 1
DWindow start: 0, count: 5
Attempts:
2 left
💡 Hint
Sliding windows overlap and advance by 1 second, so events can be counted in multiple windows.
🔧 Debug
advanced
2:00remaining
Identify the error in hopping window definition
The following Kafka Streams code attempts to create a hopping window of size 10 seconds that advances every 5 seconds. What error will this code produce?
Kafka
KStream<String, String> input = builder.stream("input-topic");

KTable<Windowed<String>, Long> counts = input
  .groupByKey()
  .windowedBy(HoppingWindows.ofSizeWithNoGrace(Duration.ofSeconds(10)).advanceBy(Duration.ofSeconds(5)))
  .count();
ANo error, code runs correctly and produces hopping windows
BRuntime error: NullPointerException due to missing grace period
CCompilation error: method advanceBy() does not exist for HoppingWindows.ofSizeWithNoGrace()
DCompilation error: missing import for Duration class
Attempts:
2 left
💡 Hint
Check the Kafka Streams API for the correct way to specify advance interval for hopping windows.
📝 Syntax
advanced
2:00remaining
Correct syntax for session window with inactivity gap
Which of the following Kafka Streams code snippets correctly creates a session window with an inactivity gap of 10 seconds?
ASessionWindows.with(Duration.ofSeconds(10))
BSessionWindows.ofInactivityGapWithNoGrace(Duration.ofSeconds(10))
CSessionWindows.withInactivityGap(Duration.ofSeconds(10))
DSessionWindows.withGap(Duration.ofSeconds(10))
Attempts:
2 left
💡 Hint
Check the official Kafka Streams API for the correct method to specify session window gap.
🚀 Application
expert
2:00remaining
Number of windows created by hopping window
If you have a hopping window of size 12 seconds that advances every 4 seconds, how many overlapping windows will an event at timestamp 20 seconds belong to?
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Calculate how many windows of size 12 seconds, advancing every 4 seconds, cover the timestamp 20.