Find the Quiet Students in All Exams — LeetCode 1412 Python Solution

HardLeetCode PremiumDatabase
Problem
#1412
Reading time
5 min

Table schema

SQL
Table: Student +---------------------+---------+ | Column Name | Type | +---------------------+---------+ | student_id | int | | student_name | varchar | +---------------------+---------+ student_id is the primary key (column with unique values) for this table. student_name is the name of the student.

Example

SQL
+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| student_id          | int     |
| student_name        | varchar |
+---------------------+---------+
student_id is the primary key (column with unique values) for this table.
student_name is the name of the student.

Python solution

Python
import duckdb
import pandas as pd

def solution(student: pd.DataFrame, exam: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Student", student)
    con.register("Exam", exam)
    return con.execute("""WITH
    T AS (
        SELECT
            student_id,
            RANK() OVER (
                PARTITION BY exam_id
                ORDER BY score
            ) AS rk1,
            RANK() OVER (
                PARTITION BY exam_id
                ORDER BY score DESC
            ) AS rk2
        FROM Exam
    )
SELECT student_id, student_name
FROM
    T
    JOIN Student USING (student_id)
GROUP BY 1
HAVING SUM(rk1 = 1) = 0 AND SUM(rk2 = 1) = 0
ORDER BY 1;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1412. Find the Quiet Students in All Exams?
LeetCode 1412. Find the Quiet Students in All Exams is rated Hard on LeetCode.
What topics does LeetCode 1412. Find the Quiet Students in All Exams cover?
LeetCode 1412. Find the Quiet Students in All Exams is tagged Database on LeetCode.
Is LeetCode 1412. Find the Quiet Students in All Exams a premium problem?
Yes. LeetCode 1412. Find the Quiet Students in All Exams 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