Java Coding Interview(non-leetcode-style) - Top 10 Active Users by Login & Email Trust
Solving non-leetcode style Java coding problems
🚀 Java + Spring Boot + SQL Interview + Certification Prep 🚀
🎯 Perfect for aspiring developers, job switchers & interview prep warriors!🔥 What’s Inside (PDF format):
Grokking the Java Interview (Vol 1 & 2)
Grokking the Spring Boot Interview
Grokking the SQL Interview
Java SE 17 Developer Certification (1Z0–829) Guide
250+ Spring Framework Certification Practice Questions
✅ Bonus: Free Sample Copies of Java, Spring Boot & Spring Questions
Problem
You’re building a user analytics system. Given a list of users, your task is to select the top 10 trusted active users based on certain rules.
User
class:
Requirements:
Write a method:
public List<String> getTop10TrustedActiveEmails(List<User> users)
that returns a list of up to 10 email addresses satisfying all of the following logic:
Filtering Rules: Only include users where:
isActive == true
email != null
,lastLoginDate != null
lastLoginDate
is within the last 30 days from todayloginCountLast30Days >= 5
Deduplication Rules: Deduplicate by email.
If the same email appears more than once, keep the one with:
- The latestlastLoginDate
- If tied, choose the one withemailVerified == true
- If tied again, choose the one with the highestloginCountLast30Days
- If still tied, choose the lexicographically smalleruserId
Final Ranking:
Sort the resulting users by:
loginCountLast30Days
descendingThen by
lastLoginDate
descending
Return:
A list of email addresses (no duplicates)
Max size: 10
Solution
Filter Active Users
As per the problem statement, we first need to filter only trusted active users. We can apply a filter over a stream of users and add conditions mentioned in the requirement list:
We are making sure the user is active, email and last login are present, last time login is not more than 30 days, and frequency of login is at least 5 times.
Apply Deduplication
We need to make sure that we don’t return the duplicated result in the output. Our requirement clearly lists all the criteria with tie-breaker logic.
We are grouping all the filtered users by their email, and for values, we are keeping the user instance using `Function.identity()`.
Next, we are adding a bunch of tie-breaker logic to satisfy the requirements. At first, we check if the last login date is not equal; if they are equal, then we move to the next tie breaker logic, which is email verification, we move to login count, and eventually, we move to userId.
At the end, we return all the values of a map, which is a Collection of user objects after deduplication.
Applying Ranking
Now we can tackle the final requirement of sorting the result.
We are sorting the result by login count in the last 30 days and last login date both in descending order, then we pick the first 10 results, and map each user to their email because that's what we need to return as a list.
Complete Solution
Let’s verify the logic.
We create a list of test users with different properties and then pass it to our `getTop10TrustedActiveEmails` method.
Output
i@example.com
e@example.com
j@example.com
a@example.com
g@example.com
f@example.com
k@example.com
c@example.com
h@example.com