Engineering With Java

Engineering With Java

Java Coding Problem - Returning Subscribers

Suraj Mishra's avatar
Suraj Mishra
Jan 01, 2026
∙ Paid

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> events representing subscription activity in 2025.

  • Output: Map<Integer, Long> mapping month (1–12) → number of returning subscribers.

Rules:

  1. For each user, consider their subscription activity across months.

  2. 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.

  3. Multiple events in the same month count as one returning subscriber for that month.

  4. 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/Spring Boot insights every week — from practical code tips to real-world use-case based interview questions.

Join 5000+ subscribers and level up your Spring & backend skills with hand-crafted content — no fluff.

First 100 paid subscribers will get the annual membership at $50/year ( 60 already converted to paid, 40 remaining )

So far we have covered 44+ real world based interview questions and will add up to 100 by end of this year.

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.

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture