MediumNo. 0550

Game Play Analysis IV — LeetCode 550 Python Solution

Topics
Database
Reading time
2 min

The problem

Work out the fraction of players who logged in again on the day immediately after their very first login, rounded to two decimal places. The Activity table has player_id (int), device_id (int), event_date (date) and games_played (int), with (player_id, event_date) as its primary key. Return a single column named fraction.

Example

Input
Activity(player_id, device_id, event_date, games_played) = (1, 2, '2023-01-03', 5), (1, 2, '2023-01-04', 6), (2, 3, '2023-02-11', 1), (3, 1, '2023-01-05', 0), (3, 4, '2023-01-09', 4)
Output
fraction = 0.33
Explanation
Player 1 is the only one who logged in the day after their first login, and 1 / 3 rounds to 0.33.

Python solution

PythonSolution
import pandas as pd


def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame:
    activity["first"] = activity.groupby("player_id").event_date.transform(min)
    activity_2nd_day = activity[
        activity["first"] + pd.DateOffset(1) == activity["event_date"]
    ]

    return pd.DataFrame(
        {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]}
    )

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

Frequently asked questions

How hard is LeetCode 550. Game Play Analysis IV?

LeetCode 550. Game Play Analysis IV is rated Medium on LeetCode.

What topics does LeetCode 550. Game Play Analysis IV cover?

LeetCode 550. Game Play Analysis IV is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.