Skip to main content

Select Data - Solution & Explanation

Easy10 min readAsked at: Microsoft, Google, Bloomberg
Practice this problem

Problem Statement

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.

Approach Overview

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 1: Approach 1: Iterate and Filter

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Approach 2: HashMap/Dictionary Lookup

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'.

Code

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), as hash table operations (get) average constant time.

Space Complexity: O(n), space usage increases with input size.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterate and FilterO(n)O(1) or O(k)Best for a single pass through the dataset or when filtering is done once.
HashMap / Dictionary LookupO(n) build + O(1) lookupO(n)Useful when selecting data by key repeatedly or handling multiple queries efficiently.

Video Solution

2880. Select Data | LeetCode | Python | Pandas • You Data And AI • 880 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Select Data easy or hard?
Select Data is generally classified as an easy problem. The core idea is straightforward filtering using iteration. The HashMap variation introduces a common optimization pattern but remains beginner-friendly and focuses on fundamental data structure usage.
Select Data Python/Java solution
In Python, you typically iterate through the list and append matching elements to a result list or use dictionary lookups for faster retrieval. In Java, a loop over the collection with conditional checks works for filtering, while a HashMap<Integer, Record> can store key-to-record mappings for constant-time access.
How to solve Select Data in O(n)?
Traverse the dataset once and apply a conditional check to each record. If the record satisfies the selection rule, add it to the result. Since each element is processed exactly once, the algorithm runs in O(n) time with constant auxiliary space.
What is the best approach for Select Data?
The most practical solution is a single pass iteration that filters records matching the required condition. This runs in O(n) time and requires minimal extra memory. If the problem involves repeated lookups by a key or identifier, building a HashMap first allows O(1) average-time access for each query.
Is Select Data asked at Google/Amazon/Meta?
Problems involving filtering datasets and using hash-based lookups appear frequently in interviews at large tech companies. While the exact problem title may vary, the underlying pattern of linear scanning or HashMap-based retrieval is commonly tested in coding interviews.
What data structure is used in Select Data?
The simplest implementation uses arrays or lists for sequential traversal. For optimized retrieval, a HashMap (dictionary) is used to map keys to records, enabling constant-time lookups and avoiding repeated scans of the dataset.
What is the time complexity of Select Data?
The standard filtering solution runs in O(n) time because every record must be checked once. Space complexity is O(1) if only scanning the dataset, or O(k) if storing the filtered results. A HashMap-based approach still requires O(n) preprocessing but enables O(1) average lookup afterward.

Ready to solve this problem?

Practice Select Data with our built-in code editor and test cases.

Practice on FleetCode