Get the Second Most Recent Activity - Problem

Imagine you're building a user activity dashboard that tracks what your users have been up to lately. You have a database table called UserActivity that logs every activity a user performs along with start and end dates.

Your mission: Find the second most recent activity for each user. But here's the catch - if a user only has one activity on record, that single activity should be returned instead.

Think of it like looking at someone's recent history - you want to see what they did before their most recent action, but if they're a new user with only one activity, that's all you can show.

Table Structure:

Column NameType
usernamevarchar
activityvarchar
startDateDate
endDateDate

Key Rules:

  • Users cannot perform multiple activities simultaneously
  • The table may contain duplicate rows
  • Return results in any order
  • For users with only 1 activity, return that activity
  • For users with 2+ activities, return the second most recent

Input & Output

example_1.sql โ€” Basic Case
$ Input: UserActivity table: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | Alice | Travel | 2020-02-12 | 2020-02-20 | | Alice | Dancing | 2020-02-21 | 2020-02-23 | | Alice | Study | 2020-02-24 | 2020-02-25 | | Bob | Travel | 2020-02-11 | 2020-02-18 | +----------+----------+------------+------------+
โ€บ Output: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | Alice | Dancing | 2020-02-21 | 2020-02-23 | | Bob | Travel | 2020-02-11 | 2020-02-18 | +----------+----------+------------+------------+
๐Ÿ’ก Note: Alice has 3 activities. Her most recent is Study (ends 2020-02-25), so her second most recent is Dancing (ends 2020-02-23). Bob has only 1 activity, so we return his only activity: Travel.
example_2.sql โ€” Multiple Users
$ Input: UserActivity table: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | John | Coding | 2023-01-01 | 2023-01-02 | | John | Reading | 2023-01-03 | 2023-01-04 | | Mary | Gym | 2023-01-01 | 2023-01-02 | | Mary | Cooking | 2023-01-05 | 2023-01-06 | | Mary | Shopping | 2023-01-07 | 2023-01-08 | +----------+----------+------------+------------+
โ€บ Output: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | John | Coding | 2023-01-01 | 2023-01-02 | | Mary | Cooking | 2023-01-05 | 2023-01-06 | +----------+----------+------------+------------+
๐Ÿ’ก Note: John has 2 activities: Reading (most recent) and Coding (second most recent). Mary has 3 activities: Shopping (most recent), Cooking (second most recent), Gym (oldest). We return the second most recent for both.
example_3.sql โ€” Edge Case: All Single Activities
$ Input: UserActivity table: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | Tom | Running | 2023-05-01 | 2023-05-01 | | Lisa | Painting | 2023-05-02 | 2023-05-03 | | Sam | Gaming | 2023-05-04 | 2023-05-05 | +----------+----------+------------+------------+
โ€บ Output: +----------+----------+------------+------------+ | username | activity | startDate | endDate | +----------+----------+------------+------------+ | Tom | Running | 2023-05-01 | 2023-05-01 | | Lisa | Painting | 2023-05-02 | 2023-05-03 | | Sam | Gaming | 2023-05-04 | 2023-05-05 | +----------+----------+------------+------------+
๐Ÿ’ก Note: Each user has exactly one activity, so we return that single activity for each user. This tests the edge case where no user has a 'second' activity.

Visualization

Tap to expand
๐Ÿ“ฑ Social Media Timeline Analysis๐Ÿ‘ค Alice's TimelineStudy (R1)Dancing (R2)Travel (R3)Count = 3 โ†’ Return Rank 2๐Ÿ‘ค Bob's TimelineTravel (R1)Count = 1 โ†’ Return Rank 1๐ŸŽฏ Final ResultsAlice โ†’ DancingBob โ†’ Travel(Second most recent) (Only activity)๐Ÿ’ก Key InsightWindow functions rank activities efficiently in one pass!
Understanding the Visualization
1
Scan User Activities
Read through all user activities with their timestamps
2
Rank by Recency
For each user, rank their activities: 1 = most recent, 2 = second most recent, etc.
3
Count Activities
Count how many total activities each user has performed
4
Apply Logic
If count = 1, take rank 1. If count > 1, take rank 2
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with ROW_NUMBER() and COUNT() enable elegant handling of both single-activity users and multi-activity users in a single efficient query, avoiding complex subqueries and multiple table scans.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single table scan with sorting for ROW_NUMBER() window function

n
2n
โšก Linearithmic
Space Complexity
O(n)

Database needs to store temporary results for window function calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Number of rows โ‰ค 104
  • username contains only lowercase letters and numbers
  • activity is a non-empty string
  • startDate โ‰ค endDate for all rows
  • Users cannot have overlapping activities (no simultaneous activities)
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
42.5K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen