Students With Invalid Departments — LeetCode 1350 Python Solution

EasyLeetCode PremiumDatabase
Problem
#1350
Reading time
2 min

Table schema

SQL
Table: Departments +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ In SQL, id is the primary key of this table. The table has information about the id of each department of a university.

Example

SQL
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
In SQL, id is the primary key of this table.
The table has information about the id of each department of a university.

Python solution

Python
import duckdb
import pandas as pd

def solution(departments: pd.DataFrame, students: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Departments", departments)
    con.register("Students", students)
    return con.execute("""SELECT id, name
FROM Students
WHERE department_id NOT IN (SELECT id FROM Departments);""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1350. Students With Invalid Departments?
LeetCode 1350. Students With Invalid Departments is rated Easy on LeetCode.
What topics does LeetCode 1350. Students With Invalid Departments cover?
LeetCode 1350. Students With Invalid Departments is tagged Database on LeetCode.
Is LeetCode 1350. Students With Invalid Departments a premium problem?
Yes. LeetCode 1350. Students With Invalid Departments 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