Count Distinct Users by Activity
Updated
Problem statement
Given a collection of user-activity records, count how many distinct users performed each activity.
Each source record is represented as exactly three strings in this order:
- decimal
user_id timestampactivity
Return the mapping as a two-column String[][]. Each row must be [activity, distinctUserCount], where the count is a decimal string. Order the rows by the first appearance of each activity in records.
Repeated records or repeated occurrences of the same activity by one user count that user only once for that activity. The timestamp is part of the input record but does not change the distinct-user count. If records is empty, return an empty matrix.
Function
countDistinctUsersByActivity(records: String[][]) → String[][]Examples
Example 1
records = [["1","2024-07-26 10:00:00","login"],["2","2024-07-26 10:05:00","login"],["1","2024-07-26 10:10:00","view"],["1","2024-07-26 10:15:00","purchase"],["2","2024-07-26 10:20:00","view"],["3","2024-07-26 10:25:00","login"],["1","2024-07-26 10:30:00","logout"]]return = [["login","3"],["view","2"],["purchase","1"],["logout","1"]]The distinct users are {1, 2, 3} for login, {1, 2} for view, and {1} for both purchase and logout. The activities first appear in the order login, view, purchase, logout.
Example 2
records = [["10","2024-08-01 09:00:00","search"],["10","2024-08-01 09:01:00","search"],["20","2024-08-01 09:02:00","search"],["10","2024-08-01 09:03:00","checkout"],["20","2024-08-01 09:04:00","checkout"]]return = [["search","2"],["checkout","2"]]User 10 performs search twice but contributes once to that activity's distinct-user count. Both users perform search and checkout.
More Google Phone Screen questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Two SumBNSF Railway · PHONE SCREENBNSF Railway interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions