Concatenate the Name and the Profession — LeetCode 2504 Python Solution

EasyLeetCode PremiumDatabase
Problem
#2504
Reading time
2 min

Table schema

SQL
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | person_id | int | | name | varchar | | profession | ENUM | +-------------+---------+ person_id is the primary key (column with a unique value) for this table. Each row in this table contains a person's ID, name, and profession.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| person_id   | int     |
| name        | varchar |
| profession  | ENUM    |
+-------------+---------+
person_id is the primary key (column with a unique value) for this table.
Each row in this table contains a person's ID, name, and profession.
The profession column in an enum of the type ('Doctor', 'Singer', 'Actor', 'Player', 'Engineer', or 'Lawyer')

Python solution

Python
import duckdb
import pandas as pd

def solution(person: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Person", person)
    return con.execute("""SELECT person_id, CONCAT(name, "(", SUBSTRING(profession, 1, 1), ")") AS name
FROM Person
ORDER BY person_id DESC;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 2504. Concatenate the Name and the Profession?
LeetCode 2504. Concatenate the Name and the Profession is rated Easy on LeetCode.
What topics does LeetCode 2504. Concatenate the Name and the Profession cover?
LeetCode 2504. Concatenate the Name and the Profession is tagged Database on LeetCode.
Is LeetCode 2504. Concatenate the Name and the Profession a premium problem?
Yes. LeetCode 2504. Concatenate the Name and the Profession 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