EasyNo. 1435LeetCode Premium

Create a Session Bar Chart — LeetCode 1435 Python Solution

Topics
Database
Reading time
3 min

Table schema

SQLTable schema
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

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

PythonSolution
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

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 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.

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