Game Play Analysis I - Problem

You are given a table Activity that shows the activity of players of some games. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.

Write a solution to find the first login date for each player.

Return the result table in any order.

Table Schema

Activity
Column Name Type Description
player_id PK int Unique identifier for each player
device_id int Device used by the player
event_date PK date Date when the player logged in
games_played int Number of games played in the session
Primary Key: (player_id, event_date)
Note: Each row represents a gaming session for a player on a specific date

Input & Output

Example 1 — Multiple Logins Per Player
Input Table:
player_id device_id event_date games_played
1 2 2016-03-01 5
1 2 2016-05-02 6
2 3 2017-06-25 1
3 1 2016-03-02 0
3 4 2018-07-03 5
Output:
player_id first_login
1 2016-03-01
2 2017-06-25
3 2016-03-02
💡 Note:

Player 1 has two login records (2016-03-01 and 2016-05-02), so we take the earlier date. Player 2 has only one record. Player 3 has two records (2016-03-02 and 2018-07-03), so we take the earlier date.

Example 2 — Single Login Per Player
Input Table:
player_id device_id event_date games_played
1 1 2020-01-15 3
2 2 2020-02-20 1
Output:
player_id first_login
1 2020-01-15
2 2020-02-20
💡 Note:

Each player has only one login record, so their first_login is simply their only event_date.

Example 3 — Same Player Different Devices
Input Table:
player_id device_id event_date games_played
1 1 2019-12-01 2
1 2 2019-11-15 4
1 3 2019-12-15 1
Output:
player_id first_login
1 2019-11-15
💡 Note:

Player 1 used different devices on different dates. We find their earliest login date regardless of device used.

Constraints

  • 1 ≤ player_id ≤ 10^5
  • 1 ≤ device_id ≤ 10^4
  • event_date is a valid date
  • 0 ≤ games_played ≤ 1000

Visualization

Tap to expand
Game Play Analysis I - Solution INPUT Activity Table player_id device_id login_date games 1 2 2016-03-01 5 1 1 2016-05-02 6 2 3 2017-06-25 1 3 1 2016-03-02 0 3 4 2018-07-03 5 Players with multiple logins: P1 2 logins P2 1 login P3 2 logins Find earliest date per player ALGORITHM STEPS 1 GROUP BY player_id Group all rows by player 2 Apply MIN(event_date) Find minimum date per group 3 Alias as first_login Rename result column 4 Return result Output player_id, first_login SQL Query: SELECT player_id, MIN(event_date) AS first_login FROM Activity GROUP BY player_id Time: O(n) | Space: O(n) FINAL RESULT Output Table player_id first_login 1 2016-03-01 2 2017-06-25 3 2016-03-02 OK - Complete! First Login Summary: Player 1 --> 2016-03-01 Player 2 --> 2017-06-25 Player 3 --> 2016-03-02 3 unique players found Key Insight: The MIN() aggregate function combined with GROUP BY efficiently finds the earliest login date for each player. This single-pass solution avoids subqueries and self-joins, making it optimal with O(n) time complexity. Alternative: Use window function ROW_NUMBER() OVER (PARTITION BY player_id ORDER BY event_date) = 1 TutorialsPoint - Game Play Analysis I | Optimal Solution
Asked in
Facebook 28 Amazon 22 Google 15
67.5K Views
High Frequency
~8 min Avg. Time
892 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