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.
The key idea in #2880 Select Data is to retrieve only the required information from a dataset by applying the correct selection conditions. In most database-style problems, this is done using a SELECT statement combined with filtering conditions such as WHERE. Instead of processing every column or unnecessary rows, the goal is to extract just the fields that satisfy the given criteria.
Start by identifying which columns are needed in the output and which conditions determine whether a row should be included. Then construct a query that selects those columns while applying logical filters (such as equality checks or boolean conditions). This ensures the dataset is reduced efficiently and only the relevant records are returned.
Since the database typically scans rows to evaluate the conditions, the time complexity is proportional to the number of rows being checked. Proper indexing in real-world systems can further optimize this process, but the conceptual approach remains a straightforward filtering operation.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Filtering rows using SELECT with conditions | O(n) | O(1) |
Ashish Pratap Singh
Use these hints if you're stuck. Try solving on your own first.
Consider applying both row and column filtering to select the desired data.
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.
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.
1#include <iostream>
2#include <vector>
3#include <string>
4
5struct Student {
6 int student_id;
7 std::string name;
8 int age;
9};
10
11void findStudent(const std::vector<Student>& students) {
12 for (const auto& student : students) {
13 if (student.student_id == 101) {
14 std::cout << "+---------+-----+\n";
std::cout << "| " << student.name << " | " << student.age << " |\n";
std::cout << "+---------+-----+\n";
break;
}
}
}
int main() {
std::vector<Student> students = {
{101, "Ulysses", 13},
{53, "William", 10},
{128, "Henry", 6},
{3, "Henry", 11}
};
findStudent(students);
return 0;
}In C++, we use a struct to define the Student type, and a vector to hold all student records. The findStudent() function iterates through the vector to find the target student ID, then prints out the 'name' and 'age'.
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.
Time Complexity: O(1), as hash table operations (get) average constant time.
Space Complexity: O(n), space usage increases with input size.
1using System;
2using System.Collections.Generic;
public class Student {
public string Name { get; set; }
public int Age { get; set; }
}
public class Program {
public static void Main() {
Dictionary<int, Student> studentsMap = new Dictionary<int, Student> {
{101, new Student {Name = "Ulysses", Age = 13}},
{53, new Student {Name = "William", Age = 10}},
{128, new Student {Name = "Henry", Age = 6}},
{3, new Student {Name = "Henry", Age = 11}}
};
if (studentsMap.TryGetValue(101, out Student student)) {
Console.WriteLine("+---------+-----+");
Console.WriteLine($"| {student.Name} | {student.Age} |");
Console.WriteLine("+---------+-----+");
}
}
}Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, basic data selection and filtering problems frequently appear in SQL interview rounds. Companies often test whether candidates understand SELECT statements, filtering logic, and efficient data retrieval.
The optimal approach is to use a SELECT query that retrieves only the required columns while applying filtering conditions with a WHERE clause. This minimizes unnecessary data processing and ensures only relevant rows are returned.
The problem primarily relies on relational database querying concepts. It uses table structures and filtering operations to select rows that satisfy certain conditions.
In the simplest case, the database scans all rows to evaluate the condition, resulting in O(n) time complexity. With proper indexing, real-world databases can significantly reduce the effective lookup time.
In C#, a Dictionary maps the unique student_id to Student objects. The TryGetValue method helps in obtaining the student with ID 101 efficiently and securely.