Create a Session Bar Chart — LeetCode 1435 Python Solution

EasyLeetCode PremiumDatabase
Problem
#1435
Reading time
3 min

Table schema

SQL
Table: Sessions +---------------------+---------+ | Column Name | Type | +---------------------+---------+ | session_id | int | | duration | int | +---------------------+---------+ session_id is the column of unique values for this table. duration is the time in seconds that a user has visited the application.

Example

SQL
+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| session_id          | int     |
| duration            | int     |
+---------------------+---------+
session_id is the column of unique values for this table.
duration is the time in seconds that a user has visited the application.

Python solution

Python
import duckdb
import pandas as pd

def solution(sessions: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Sessions", sessions)
    return con.execute("""SELECT '[0-5>' AS bin, COUNT(1) AS total FROM Sessions WHERE duration < 300
UNION
SELECT '[5-10>' AS bin, COUNT(1) AS total FROM Sessions WHERE 300 <= duration AND duration < 600
UNION
SELECT '[10-15>' AS bin, COUNT(1) AS total FROM Sessions WHERE 600 <= duration AND duration < 900
UNION
SELECT '15 or more' AS bin, COUNT(1) AS total FROM Sessions WHERE 900 <= duration;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1435. Create a Session Bar Chart?
LeetCode 1435. Create a Session Bar Chart is rated Easy on LeetCode.
What topics does LeetCode 1435. Create a Session Bar Chart cover?
LeetCode 1435. Create a Session Bar Chart is tagged Database on LeetCode.
Is LeetCode 1435. Create a Session Bar Chart a premium problem?
Yes. LeetCode 1435. Create a Session Bar Chart 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