Quick revision
The short answer
Fixed 5-minute periods are tumbling windows, a moving 1-hour count updated every minute is a hopping window, shopping sessions ending after 30 minutes of inactivity are session windows, and two payments within 10 seconds of each other are found with a sliding window. By default Streams uses each record's own timestamp, which is the producer's CreateTime or the broker's LogAppendTime depending on the topic, and a custom TimestampExtractor is needed to use a time field from the payload.
What to remember
In this answer, we matched four requirements to window types: tumbling windows for fixed, non-overlapping periods; hopping windows for a moving count, where each record lands in size divided by advance windows; session windows for activity separated by idle gaps, which can merge when late records bridge them; and sliding windows for records close in time, without the duplicates of a fine-grained hopping window. Streams uses the record timestamp by default, CreateTime or LogAppendTime, and a TimestampExtractor lets us use a time from the payload.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Fixed 5-minute periods are tumbling windows, a moving 1-hour count updated every minute is a hopping window, shopping sessions ending after 30 minutes of inactivity are session windows, and two payments within 10 seconds of each other are found with a sliding window. By default Streams uses each record's own timestamp, which is the producer's CreateTime or the broker's LogAppendTime depending on the topic, and a custom TimestampExtractor is needed to use a time field from the payload.
Why this question is asked
Windows decide what a count means. Pick a hopping window where a tumbling one was meant and every event is counted many times; pick a tumbling window for a 10-second rule and pairs that straddle a boundary go unseen. Interviewers ask this to check that we can match a business rule to the right window type and know which clock Streams uses. In this answer, we will recap windowing and grace, go through the four requirements one by one with code, compare them side by side, and finish with timestamps.
1A Quick Recap of Windowed Aggregation
A windowed aggregation groups records by key and by a time range, and computes a result per key per window. Streams uses event time, the timestamp on each record, not the time the record is processed, so windows mean the same thing when we replay last week's data.
Because records can arrive out of order, every window type takes a grace period: how long, in stream time, a window keeps accepting late records after it ends. ofSizeWithNoGrace means zero grace; the AndGrace variants take an explicit Duration. Stream time is the highest record timestamp a task has seen so far.
2(a) Page Views per 5-Minute Period: Tumbling
Tumbling windows are fixed-size, non-overlapping and aligned to the epoch, so the windows are 10:00 to 10:05, 10:05 to 10:10 and so on, and every record belongs to exactly one window. That matches per fixed period exactly.
3(b) A Moving 1-Hour Count Updated Every Minute: Hopping
Hopping windows have a size and an advance. With a size of 1 hour advancing by 1 minute, a new window starts every minute and each lasts an hour, so windows overlap and every record belongs to 60 of them. That is what a moving count updated every minute means, and it is also its cost: each failed login updates 60 window counts and the store holds 60 times as many windows per key.
4(c) Shopping Sessions: Session Windows
Session windows have no fixed size. Each key's records within 30 minutes of each other form one session, and a gap of more than 30 minutes ends it. Sessions are per key, so two users' sessions are independent.
There is a subtle behaviour: if a late record arrives that falls between two sessions and is within 30 minutes of both, Streams merges them into one. It emits deletes (tombstones) for the two old sessions and a result for the merged one, so downstream must handle session results that disappear.
5(d) Two Payments Within 10 Seconds: Sliding Windows
A tumbling 10-second window would miss payments at 10:00:08 and 10:00:12, because they fall into different windows. A hopping window with a 1-second advance would catch them but create ten overlapping windows per record, most of them duplicates. Sliding windows group records whose timestamps lie within a given difference of each other, creating windows only where records actually are, which is exactly the rule.
| Requirement | Window | Records per window |
|---|---|---|
| Views per fixed 5 minutes | Tumbling | Each record in exactly one window |
| Moving 1-hour count, every minute | Hopping (1 h, advance 1 min) | Each record in 60 windows |
| Session ends after 30 min idle | Session | Variable size per key; may merge |
| Two payments within 10 s | Sliding (10 s difference) | Only windows where records are |
A windowed self-join of the payments stream with JoinWindows.ofTimeDifferenceAndGrace(10 seconds, grace) is an alternative when we want the pair of payments itself rather than a count.
6Which Timestamp Streams Uses
By default Streams uses the timestamp stored in the record, read by the FailOnInvalidTimestamp extractor, which fails on negative timestamps. What that timestamp means depends on the topic: with message.timestamp.type=CreateTime (the default) it is the time the producer set, usually when the event was created; with LogAppendTime it is the time the broker appended the record.
If the real event time is a field in the payload, for example a phone's local time of a page view, we write a TimestampExtractor that reads it and register it with default.timestamp.extractor, or per source with Consumed.withTimestampExtractor. That choice changes which window every record lands in.
7Follow-up questions
What is stream time, and how does it differ from wall-clock time?
Stream time is the largest record timestamp a task has seen so far; it moves only when records arrive, and never backwards. Wall-clock time is the system clock. Windows close, grace periods expire and suppressed results are emitted based on stream time, so a quiet partition can keep a window open for hours of real time, while a replay of a day's data can close thousands of windows in seconds.
Are tumbling windows aligned to my time zone?
No. They are aligned to the epoch in UTC, so 1-day tumbling windows run from midnight UTC. For business days in a local time zone, we either compute a day key ourselves and aggregate by it, or use a custom windowing approach.
How much state does a hopping window cost?
Roughly size divided by advance times what a tumbling window would keep, here 60 times, plus the grace period. That is why sliding windows are preferred for small time differences.
8Conclusion
In this answer, we matched four requirements to window types: tumbling windows for fixed, non-overlapping periods; hopping windows for a moving count, where each record lands in size divided by advance windows; session windows for activity separated by idle gaps, which can merge when late records bridge them; and sliding windows for records close in time, without the duplicates of a fine-grained hopping window. Streams uses the record timestamp by default, CreateTime or LogAppendTime, and a TimestampExtractor lets us use a time from the payload.
Open with
For page views per fixed five-minute period I would use a tumbling window, TimeWindows.ofSizeWithNoGrace with five minutes, or ofSizeAndGrace to accept late records; tumbling windows are fixed, non-overlapping and aligned to the epoch, so each record falls in exactly one.
Then, if they want more
For a moving one-hour count updated every minute I would use a hopping window: ofSizeAndGrace with one hour and a grace, then advanceBy one minute. The windows overlap, so each record updates sixty windows and state grows with that.
For shopping sessions I would use SessionWindows.ofInactivityGapAndGrace with thirty minutes; sessions are per key with no fixed size, and a late record that bridges two sessions merges them, so Streams emits deletes for the old ones and a result for the merged session.
For two payments from the same card within ten seconds I would use SlidingWindows.ofTimeDifferenceAndGrace with ten seconds and count at least two; it only creates windows where records are, unlike a hopping window with a tiny advance, and a windowed self-join is an alternative.
By default Streams uses the record's timestamp, which is the producer's CreateTime or the broker's LogAppendTime depending on the topic, and to use a time from the payload I set a custom TimestampExtractor with default.timestamp.extractor.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "For each requirement choose a Kafka Streams window type and write its definition: (a)...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/6316698ac0/