MediumNo. 1990LeetCode Premium

Count the Number of Experiments — LeetCode 1990 Python Solution

Topics
Database
Reading time
6 min

Table schema

SQLTable schema
Table: Experiments +-----------------+------+ | Column Name | Type | +-----------------+------+ | experiment_id | int | | platform | enum | | experiment_name | enum | +-----------------+------+ experiment_id is the column with unique values for this table. platform is an enum (category) type of values ('Android', 'IOS', 'Web').

Example

SQLExample
+-----------------+------+
| Column Name     | Type |
+-----------------+------+
| experiment_id   | int  |
| platform        | enum |
| experiment_name | enum |
+-----------------+------+
experiment_id is the column with unique values for this table.
platform is an enum (category) type of values ('Android', 'IOS', 'Web').
experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').
This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.

Python solution

PythonSolution
import duckdb
import pandas as pd

def solution(experiments: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Experiments", experiments)
    return con.execute("""WITH
    P AS (
        SELECT 'Android' AS platform
        UNION
        SELECT 'IOS'
        UNION
        SELECT 'Web'
    ),
    Exp AS (
        SELECT 'Reading' AS experiment_name
        UNION
        SELECT 'Sports'
        UNION
        SELECT 'Programming'
    ),
    T AS (
        SELECT *
        FROM
            P,
            Exp
    )
SELECT platform, experiment_name, COUNT(experiment_id) AS num_experiments
FROM
    T AS t
    LEFT JOIN Experiments USING (platform, experiment_name)
GROUP BY 1, 2;""").df()

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 1990. Count the Number of Experiments?

LeetCode 1990. Count the Number of Experiments is rated Medium on LeetCode.

What topics does LeetCode 1990. Count the Number of Experiments cover?

LeetCode 1990. Count the Number of Experiments is tagged Database on LeetCode.

Is LeetCode 1990. Count the Number of Experiments a premium problem?

Yes. LeetCode 1990. Count the Number of Experiments is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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.