Watch 3 video solutions for Select Data, a easy level problem. This walkthrough by You Data And AI has 874 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |