DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
Write a solution to select the name and age of the student with student_id = 101.
The result format is in the following example.
Example 1: Input: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 101 | Ulysses | 13 | | 53 | William | 10 | | 128 | Henry | 6 | | 3 | Henry | 11 | +------------+---------+-----+ Output: +---------+-----+ | name | age | +---------+-----+ | Ulysses | 13 | +---------+-----+ Explanation: Student Ulysses has student_id = 101, we select the name and age.
Problem Overview: You are given a collection of data records and a condition that determines which records should be returned. The task is to scan the dataset and select only the entries that satisfy the required criteria. The challenge is straightforward: efficiently filter the relevant data while keeping the implementation simple.
Approach 1: Iterate and Filter (Time: O(n), Space: O(1) or O(k))
The most direct strategy is a linear scan of the dataset. Iterate through every record and check whether it satisfies the required condition. If the record matches, append it to the result list. This approach relies only on basic array iteration and conditional checks, making it easy to implement in any language.
The key idea is that you don't need extra preprocessing. Each element is evaluated exactly once using a simple if condition. The time complexity is O(n) because every element must be checked, while the space complexity is O(1) excluding the output (or O(k) if you count the selected results). This approach works well when the dataset is small or when you only perform the filtering operation once.
Approach 2: HashMap / Dictionary Lookup (Time: O(n), Space: O(n))
If the selection condition depends on matching specific identifiers or keys, a hash map (dictionary) can speed up repeated lookups. First build a map where the key represents the searchable attribute (for example, an ID) and the value stores the corresponding record. Building the map takes O(n) time.
Once the map exists, selecting records becomes a constant-time lookup using the key. Each lookup runs in O(1) average time due to hashing. This approach is useful when multiple queries are performed on the same dataset or when you frequently retrieve records by a unique field. The tradeoff is additional O(n) memory for storing the hash structure.
Hash-based lookups are a common pattern in problems involving dictionary-style access. Instead of repeatedly scanning the entire dataset, you convert the data into a structure optimized for fast retrieval.
Recommended for interviews: Start with the iterative filtering approach since it directly reflects the problem statement and demonstrates clear reasoning. Then mention the HashMap optimization if the problem involves repeated lookups or key-based access. Interviewers expect candidates to recognize when linear scanning is sufficient and when a hash-based structure reduces repeated work.
Explanation: Iterator Filtering
This approach involves iterating through each record (or row) in the DataFrame and checking the 'student_id' against the specified value (101). Once a match is found, we can extract the 'name' and 'age' and return it as the result.
The function find_student() iterates through an array of Student structs, checking each student_id. When it finds a match (101), it prints out the 'name' and 'age' fields in a formatted manner.
Time Complexity: O(n), where n is the number of students. We may have to check each student in the worst case.
Space Complexity: O(1), as no additional space beyond input storage is used.
Explanation: Lookup with Map/Dictionary
This approach involves using a hash table, such as a Dictionary in Python or HashMap in Java, to store each student record with their student_id as the key. This enables direct access to a record based on student_id with average O(1) complexity, extracting 'name' and 'age' immediately.
Using a HashMap, students are stored with their ID as keys. By accessing studentMap.get(101), we retrieve the record instantly, allowing the program to directly print out the 'name' and 'age'.
Java
Python
C#
JavaScript
Time Complexity: O(1), as hash table operations (get) average constant time.
Space Complexity: O(n), space usage increases with input size.
Python
| Approach | Complexity |
|---|---|
| Approach 1: Iterate and Filter | Time Complexity: O(n), where n is the number of students. We may have to check each student in the worst case. Space Complexity: O(1), as no additional space beyond input storage is used. |
| Approach 2: HashMap/Dictionary Lookup | Time Complexity: O(1), as hash table operations (get) average constant time. Space Complexity: O(n), space usage increases with input size. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterate and Filter | O(n) | O(1) or O(k) | Best for a single pass through the dataset or when filtering is done once. |
| HashMap / Dictionary Lookup | O(n) build + O(1) lookup | O(n) | Useful when selecting data by key repeatedly or handling multiple queries efficiently. |
2880. Select Data | LeetCode | Python | Pandas • You Data And AI • 874 views views
Watch 2 more video solutions →Practice Select Data with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor