Bird
Raised Fist0
Kafkadevops~20 mins

Windowed operations in Kafka - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of windowed operations in Kafka Streams?
easy
A. To compress data for storage
B. To group data into time intervals for easier analysis
C. To encrypt data streams
D. To sort data alphabetically

Solution

  1. Step 1: Understand windowed operations

    Windowed operations group data based on time intervals, making it easier to analyze events occurring within those intervals.
  2. Step 2: Compare options

    Sorting, encrypting, and compressing are unrelated to windowing, which focuses on time-based grouping.
  3. Final Answer:

    To group data into time intervals for easier analysis -> Option B
  4. Quick Check:

    Windowed operations = time grouping [OK]
Hint: Windowed means grouping by time intervals [OK]
Common Mistakes:
  • Confusing windowing with sorting
  • Thinking windowing encrypts data
  • Assuming windowing compresses data
2. Which of the following is the correct syntax to create a tumbling window of 5 minutes in Kafka Streams DSL?
easy
A. SlidingWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5))
B. SessionWindows.of(Duration.ofMinutes(5))
C. TimeWindows.of(Duration.ofMinutes(5))
D. Windowed.of(Duration.ofMinutes(5))

Solution

  1. Step 1: Identify tumbling window syntax

    Tumbling windows in Kafka Streams are created using TimeWindows.of() with a duration.
  2. Step 2: Check options

    SessionWindows is for session windows, SlidingWindows is for sliding windows, and Windowed is not a window type class.
  3. Final Answer:

    TimeWindows.of(Duration.ofMinutes(5)) -> Option C
  4. Quick Check:

    Tumbling window = TimeWindows.of() [OK]
Hint: Tumbling windows use TimeWindows.of() syntax [OK]
Common Mistakes:
  • Using SessionWindows for tumbling windows
  • Confusing SlidingWindows with tumbling windows
  • Using non-existent Windowed class
3. Given the following Kafka Streams code snippet, what will be the output count for the window ending at timestamp 60000?
stream.groupByKey()
      .windowedBy(TimeWindows.of(Duration.ofSeconds(30)))
      .count()
      .toStream()
      .foreach((windowedKey, count) -> System.out.println(windowedKey.window().end() + ": " + count));
Assume events with keys arrive at timestamps 10000, 20000, 35000, 40000, and 55000.
medium
A. 3
B. 4
C. 2
D. 5

Solution

  1. Step 1: Identify window intervals

    Windows are 30 seconds long: [0-30000), [30000-60000), etc. Events at 10000 and 20000 fall in first window; 35000, 40000, 55000 fall in second window ending at 60000.
  2. Step 2: Count events in window ending at 60000

    Events at 35000, 40000, and 55000 are inside the window ending at 60000, so count is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Events in 30s window = 3 [OK]
Hint: Count events in the correct time window interval [OK]
Common Mistakes:
  • Counting events from previous window
  • Misunderstanding window boundaries
  • Including events outside the window
4. Identify the error in this Kafka Streams windowed count code snippet:
stream.groupByKey()
      .windowedBy(SessionWindows.of(Duration.ofMinutes(1)))
      .count()
      .toStream()
      .foreach((windowedKey, count) -> System.out.println(windowedKey.window().start() + ": " + count));
medium
A. windowedBy() cannot be used with groupByKey()
B. toStream() is not available after count()
C. count() must be called before windowedBy()
D. SessionWindows.of() requires a gap duration, not a window size

Solution

  1. Step 1: Check SessionWindows.of() usage

    SessionWindows require a gap duration (inactivity gap), not a fixed window size in of().
  2. Step 2: Validate other method calls

    groupByKey() works with windowedBy(), count() is correctly placed, and toStream() is valid after count().
  3. Final Answer:

    SessionWindows.of() requires a gap duration, not a window size -> Option D
  4. Quick Check:

    SessionWindows need gap duration [OK]
Hint: SessionWindows need gap duration, not fixed size [OK]
Common Mistakes:
  • Using fixed duration in SessionWindows.of()
  • Misplacing count() before windowedBy()
  • Assuming toStream() is invalid after count()
5. You want to count user clicks in overlapping 1-minute windows that advance every 30 seconds using Kafka Streams. Which window type and configuration should you use?
hard
A. Use TimeWindows.of(Duration.ofMinutes(1)).advanceBy(Duration.ofSeconds(30))
B. Use SlidingWindows.ofTimeDifferenceAndGrace(Duration.ofMinutes(1), Duration.ofSeconds(0)).advanceBy(Duration.ofSeconds(30))
C. Use SessionWindows.withGap(Duration.ofSeconds(30))
D. Use TimeWindows.of(Duration.ofSeconds(30)).advanceBy(Duration.ofMinutes(1))

Solution

  1. Step 1: Identify overlapping window type

    Overlapping windows that slide are created with TimeWindows.of() and advanceBy() to set the slide interval.
  2. Step 2: Match configuration to requirement

    Window size is 1 minute, sliding every 30 seconds, so TimeWindows.of(1 minute).advanceBy(30 seconds) fits perfectly.
  3. Final Answer:

    Use TimeWindows.of(Duration.ofMinutes(1)).advanceBy(Duration.ofSeconds(30)) -> Option A
  4. Quick Check:

    Sliding windows = TimeWindows with advanceBy [OK]
Hint: Sliding windows use TimeWindows with advanceBy() [OK]
Common Mistakes:
  • Using SlidingWindows class which is deprecated
  • Confusing session windows with sliding windows
  • Swapping window size and advanceBy durations
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