Watch 3 video solutions for Change Data Type, a easy level problem. This walkthrough by You Data And AI has 398 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 |
| grade | float |
+-------------+--------+
Write a solution to correct the errors:
The grade column is stored as floats, convert it to integers.
The result format is in the following example.
Example 1: Input: DataFrame students: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73.0 | | 2 | Kate | 15 | 87.0 | +------------+------+-----+-------+ Output: +------------+------+-----+-------+ | student_id | name | age | grade | +------------+------+-----+-------+ | 1 | Ava | 6 | 73 | | 2 | Kate | 15 | 87 | +------------+------+-----+-------+ Explanation: The data types of the column grade is converted to int.
Problem Overview: You are given a dataset where one or more fields are stored using the wrong data type (for example, numbers stored as strings). The task is to convert the column into the correct data type so it can be used for numeric operations, sorting, or analysis.
Approach 1: Using DataFrame Libraries (O(n) time, O(1) extra space)
Most modern languages provide high-level data processing libraries that handle type conversion efficiently. In Python, libraries like pandas allow direct casting using astype(). Similar functionality exists in JavaScript data libraries, Java data processing frameworks, and C# data table APIs. The key idea is simple: select the column and apply a built-in conversion function that transforms every value to the target type internally. These libraries are optimized in native code and perform the conversion in linear time relative to the number of rows.
This approach is concise and reliable because the library automatically handles iteration, parsing, and potential edge cases such as null values. When working with structured datasets, analytics pipelines, or tabular processing, this is the most practical solution.
Approach 2: Manual Iteration and Conversion (O(n) time, O(1) extra space)
If a DataFrame-style library is not available, you can convert values manually. Iterate through each record in the dataset, read the value as a string (or existing type), and apply a type conversion function such as int(), parseInt(), or language-specific casting operators. Replace the original value with the converted one while iterating through the collection.
This approach explicitly performs the conversion step for every element. The algorithm scans the dataset once, making the time complexity O(n). Memory usage stays constant because the conversion happens in place without allocating additional structures.
Manual iteration is useful when you are working with raw arrays, custom objects, or low-level systems where DataFrame utilities are unavailable. It also helps reinforce basic iteration and type conversion concepts that appear frequently in data processing tasks.
Recommended for interviews: The DataFrame approach is the cleanest solution in real-world data engineering environments because it uses optimized library operations. However, interviewers often expect you to explain the underlying logic. Showing the manual iteration approach demonstrates that you understand how each value is parsed and converted, while still recognizing that the optimal complexity remains O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using DataFrame Libraries | O(n) | O(1) | Best for structured datasets and analytics workflows using libraries like pandas or similar table APIs |
| Manual Iteration and Conversion | O(n) | O(1) | Useful when working with raw arrays, objects, or environments without DataFrame utilities |