Count the Number of Experiments — LeetCode 1990 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1990
Reading time
6 min

Table schema

SQL
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

SQL
+-----------------+------+
| 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

Python
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

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

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.

Get Stealth Interview