Drop Missing Data solution and explanation
The problem
DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
There are some rows having missing values in the name column.
Write a solution to remove the rows with missing values.
The result format is in the following example.
Example 1:
Input: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 32 | Piper | 5 | | 217 | None | 19 | | 779 | Georgia | 20 | | 849 | Willow | 14 | +------------+---------+-----+ Output: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 32 | Piper | 5 | | 779 | Georgia | 20 | | 849 | Willow | 14 | +------------+---------+-----+ Explanation: Student with id 217 havs empty value in the name column, so it will be removed.
Approach overview
Problem Overview: You receive a dataset (or table-like structure) that may contain missing values in some rows. The task is to remove rows with missing data and return only the valid records. The operation is common in data preprocessing where incomplete entries must be excluded before further analysis.
Approach 1: Using Filtering Method (O(n) time, O(n) space)
This approach relies on filtering the dataset and keeping only rows that contain valid values in the required column(s). You iterate through each record and check whether the target field is null, None, or otherwise missing. If the value exists, the row is added to the result collection; otherwise it is skipped. High-level languages such as Python and JavaScript make this concise with built‑in filtering utilities like DataFrame.dropna() or array.filter(). The algorithm scans the data once, giving O(n) time complexity with O(n) auxiliary space for the filtered result. This approach is straightforward and commonly used in real-world data pipelines.
Approach 2: Iterative Approach (O(n) time, O(1) extra space)
The iterative method manually traverses the dataset using a loop and removes or skips rows with missing fields. In lower-level languages such as C or C++, you check each element and copy valid rows into the output position of the same array or another buffer. The key idea is simple: iterate once, test for missing values, and keep only valid entries. If you modify the structure in place, the extra space can be reduced to O(1). This technique is useful when working with raw arrays or when memory usage must stay minimal. The runtime remains O(n) because every row is inspected exactly once.
Problems like this mainly test careful data traversal and validation rather than complex algorithms. The pattern appears frequently in preprocessing tasks and general array manipulation. Practicing these operations builds comfort with linear scans and conditional filtering, which also appear in problems involving arrays and basic iteration. The same idea can extend to more advanced filtering logic such as validating ranges, removing duplicates, or applying rules across multiple fields.
Recommended for interviews: The filtering approach is typically expected because it expresses the intent clearly and uses a single linear pass. Showing the iterative implementation demonstrates that you understand how the operation works internally. In interviews, start with the simple scan that checks each row and excludes missing values, then mention that built‑in filtering utilities provide the same O(n) performance with cleaner code.
Approach 1: Using Filtering Method
This approach leverages a filtering method to iterate over each row and eliminate the rows with missing 'name' values. The method checks for nullity in the 'name' column and keeps only those rows where 'name' is not null.
import pandas as pd
def drop_missing_data(students):
return students.dropna(subset=['name'])function dropMissingData(students) {
return students.filter(row => row.name !== null);
}In Python, Pandas provides a convenient dropna function which is used to remove missing values. Here, we specify the 'name' column in the subset parameter to ensure only rows where the 'name' is missing are dropped.
Time Complexity: O(n), where n is the number of rows, as we potentially check each row.
Space Complexity: O(1), as we are modifying the DataFrame in place (though Pandas may create a copy depending on the operation).
Approach 2: Iterative Approach
This approach manually iterates over each row in the dataset, checking if the 'name' field is missing. Rows with missing 'name' values are filtered out, which can be useful in environments that lack high-level filtering functions.
#include <iostream>
#include <vector>
using namespace std;
struct Student {
int student_id;
string name;
int age;
};
vector<Student> dropMissingData(const vector<Student>& students) {
vector<Student> validStudents;
for (const auto& student : students) {
if (!student.name.empty()) {
validStudents.push_back(student);
}
}
return validStudents;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int student_id;
char *name;
int age;
} Student;
Student* dropMissingData(Student* students, int size, int* newSize) {
Student* validStudents = malloc(size * sizeof(Student));
int index = 0;
for (int i = 0; i < size; i++) {
if (students[i].name != NULL) {
validStudents[index++] = students[i];
}
}
*newSize = index;
return validStudents;
}In C++, we use a vector to store the students. We loop through each student and add those with non-empty names to a new vector called validStudents.
Time Complexity: O(n), to iterate over the students.
Space Complexity: O(n), due to the new vector storing valid students.
Complexity comparison
| Approach | Complexity |
|---|---|
| Using Filtering Method | Time Complexity: O(n), where n is the number of rows, as we potentially check each row. |
| Iterative Approach | Time Complexity: O(n), to iterate over the students. |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Filtering Method | O(n) | O(n) | Best for high-level languages with built-in filtering utilities like Python or JavaScript |
| Iterative Approach | O(n) | O(1) extra space | Useful in C/C++ or memory-constrained environments where manual traversal is required |
Frequently asked questions
Is Drop Missing Data easy or hard?
Drop Missing Data is classified as an easy problem. The solution requires a simple linear scan and a condition to skip rows containing missing values, making it a good practice exercise for beginners learning array or dataset processing.
Drop Missing Data Python/Java solution
In Python, the problem is often solved using a DataFrame operation such as dropna on the required column, which internally performs a linear scan. In JavaScript, a similar result can be achieved with the array filter method that returns only rows where the field is not null or undefined.
How to solve Drop Missing Data in O(n)?
Iterate through the dataset and check whether the target column contains a missing value such as null or None. If the value exists, keep the row; otherwise skip it. Because the dataset is scanned only once, the overall time complexity remains O(n).
What is the best approach for Drop Missing Data?
The filtering approach is typically the best solution. You scan the dataset once and keep only rows where the required field is not null or missing. This method runs in O(n) time and is easy to implement using built-in filtering or drop functions in languages like Python or JavaScript.
Is Drop Missing Data asked at Google/Amazon/Meta?
Problems involving filtering invalid or missing records appear frequently in interviews at large tech companies because they test clean data traversal and validation logic. While the exact problem title may vary, the underlying pattern of scanning and filtering rows is common in data processing questions.
What data structure is used in Drop Missing Data?
The problem typically operates on a table-like structure such as an array of records or a DataFrame. The solution mainly relies on sequential traversal and conditional checks rather than advanced data structures.
What is the time complexity of Drop Missing Data?
The standard solution runs in O(n) time because each row in the dataset must be checked exactly once for missing values. Space complexity is usually O(n) if a new filtered collection is created, or O(1) extra space if the dataset is modified in place.



