Java Coding Problem - Returning Subscribers
Question
You work for a video streaming platform. Users can subscribe, unsubscribe, and re-subscribe at any time.
You have a log of subscription events per user with a timestamp.
You want to find, for each month, how many users are “returning subscribers.”
A returning subscriber is someone who was not subscribed in the previous month, but has at least one subscription activity in the current month.
First-time subscribers in the current month are considered returning for reporting purposes.
record SubscriptionEvent(int userId, LocalDate eventDate) {}Input: List<SubscriptionEvent>
eventsrepresenting subscription activity in 2025.Output: Map<Integer, Long> mapping month (1–12) → number of returning subscribers.
Rules:
For each user, consider their subscription activity across months.
A user counts as returning in month M if:
They have at least one subscription event in month M.
They had no subscription events in month M−1 (the previous month).
If their first subscription activity is in month M, treat them as returning.
Multiple events in the same month count as one returning subscriber for that month.
Months without any returning subscribers can be omitted or shown as zero.
Example Input
List<SubscriptionEvent> events = List.of(
new SubscriptionEvent(101, LocalDate.of(2025, 1, 5)),
new SubscriptionEvent(102, LocalDate.of(2025, 2, 15)),
new SubscriptionEvent(101, LocalDate.of(2025, 3, 20)),
new SubscriptionEvent(103, LocalDate.of(2025, 3, 10)),
new SubscriptionEvent(104, LocalDate.of(2025, 4, 5))
);
Expected Output:
1 -> 1 // user 101 first subscription
2 -> 1 // user 102 first subscription
3 -> 2 // user 101 (inactive Feb) + user 103 (first subscription)
4 -> 1 // user 104 (first subscription)
Task
Implement a method:
Map<Integer, Long> returningSubscribersPerMonth(List<SubscriptionEvent> events)📢 Get actionable Java insights every week — from practical code tips to expert interview questions you can use today.
Join 3200+ subscribers and level up your Spring & backend skills with hand-crafted content — no fluff.
Start your subscription from just $2.50/mo with an annual subscription.
Not convinced? Check out the details of the past work
Solution
First, we take events and create a stream, then we perform grouping over userId and add months to the set (to avoid duplicate months).
groupingBy(SubscriptionEvent::userId) group all events by userId.
Collectors.mapping(..., Collectors.toSet()) converts each user’s list of events into a Set of active months.



