Modify Columns solution and explanation
The problem
DataFrame employees
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| name | object |
| salary | int |
+-------------+--------+
A company intends to give its employees a pay rise.
Write a solution to modify the salary column by multiplying each salary by 2.
The result format is in the following example.
Example 1:
Input: DataFrame employees +---------+--------+ | name | salary | +---------+--------+ | Jack | 19666 | | Piper | 74754 | | Mia | 62509 | | Ulysses | 54866 | +---------+--------+ Output: +---------+--------+ | name | salary | +---------+--------+ | Jack | 39332 | | Piper | 149508 | | Mia | 125018 | | Ulysses | 109732 | +---------+--------+ Explanation: Every salary has been doubled.
Approach overview
Problem Overview: You are given a table-like dataset and need to update values in specific columns according to a rule. The goal is to modify those columns efficiently while keeping the rest of the data unchanged.
Approach 1: Loop and Update Method (O(n * m) time, O(1) extra space)
This approach iterates through each target column and updates its values row by row. You loop over the list of column names, access the column, and apply the transformation directly. The key idea is explicit iteration: read a value, compute the updated value, and write it back. Time complexity is O(n * m) where n is the number of rows and m is the number of columns being modified. Space complexity stays O(1) since the updates happen in place. This approach is straightforward and easy to debug, but Python-level loops can become slower on large datasets.
Approach 2: Vectorized Operations Method (O(n * m) time, O(1) extra space)
This method relies on vectorized operations provided by libraries like Pandas. Instead of iterating through rows manually, you select the target columns and apply the transformation to the entire column at once. Under the hood, the operation runs in optimized C-based routines, which makes it significantly faster than Python loops. The time complexity remains O(n * m), but the constant factors are much smaller due to vectorization. Space complexity stays O(1) if the update is applied in place.
Vectorized operations are common when working with tabular data structures and are widely used in data processing pipelines. Understanding when to replace explicit loops with vectorized expressions is a key performance optimization when working with Python data workflows or large array-like datasets.
Recommended for interviews: Start with the loop-based approach to demonstrate the core logic and correctness. Then move to the vectorized solution to show familiarity with optimized data operations. Interviewers generally expect the vectorized approach when working with tabular structures because it is cleaner, faster, and closer to real-world data processing patterns.
Approach 1: Loop and Update Method
This approach uses a loop to iterate through each row of the DataFrame and manually updates the 'salary' column by multiplying it by 2. This method is straightforward and helps understand the manipulation of each data point.
import pandas as pd
data = {'name': ['Jack', 'Piper', 'Mia', 'Ulysses'],
'salary': [19666, 74754, 62509, 54866]}
employees = pd.DataFrame(data)
for index, row in employees.iterrows():
employees.at[index, 'salary'] = row['salary'] * 2
print(employees)In this Python solution, we use a for loop to iterate over the DataFrame rows using the iterrows() method. For each row, we update the 'salary' column by accessing the current index and multiplying the current value by 2. This changes the original DataFrame in place.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), only a constant space is used for temporary variables.
Approach 2: Vectorized Operations Method
This approach leverages DataFrame vectorized operations, which are optimized and efficient for operations on entire columns without needing explicit loops.
import pandas as pd
data = {'name': ['Jack', 'Piper', 'Mia', 'Ulysses'],
'salary': [19666, 74754, 62509, 54866]}
employees = pd.DataFrame(data)
# Use vectorized operation
employees['salary'] *= 2
print(employees)In this Python solution, we utilize the DataFrame's ability to perform vectorized operations. Here, we directly multiply the entire 'salary' column by 2 using employees['salary'] *= 2. This operation is highly efficient and modifies the DataFrame in place.
Time Complexity: O(n), where n is the number of employees. This is performed efficiently due to the vectorized operation.
Space Complexity: O(1), as modifications are done in place.
Complexity comparison
| Approach | Complexity |
|---|---|
| Loop and Update Method | Time Complexity: O(n), where n is the number of employees. |
| Vectorized Operations Method | Time Complexity: O(n), where n is the number of employees. This is performed efficiently due to the vectorized operation. |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Loop and Update Method | O(n * m) | O(1) | Good for understanding the transformation logic or when working in environments without vectorized support |
| Vectorized Operations Method | O(n * m) | O(1) | Best for large datasets and production code using Pandas or similar columnar libraries |
Frequently asked questions
Modify Columns Python solution
A Python solution usually relies on Pandas. One option iterates through each target column and updates values with a loop. A more efficient method selects the columns directly and applies a vectorized transformation, allowing Pandas to process the entire column at once.
Is Modify Columns easy or hard?
Modify Columns is considered an easy problem. The main challenge is recognizing that column-wise vectorized operations are more efficient than row-by-row loops when working with tabular datasets.
How to solve Modify Columns in O(n)?
If only a single column is modified, the problem effectively becomes O(n) because each row is processed once. Using vectorized column operations allows the transformation to be applied across the column efficiently without explicit Python loops.
What is the best approach for Modify Columns?
The vectorized operations approach is the best in practice. It applies the transformation to entire columns at once using optimized Pandas operations, avoiding slow Python loops. This keeps the time complexity at O(n * m) but significantly improves performance for large datasets.
Is Modify Columns asked at Google/Amazon/Meta?
Problems involving column transformations and vectorized operations appear frequently in data-focused interviews and roles that use Python or Pandas. While this exact question may vary, similar data manipulation tasks are common in data engineering and analytics interviews.
What data structure is used in Modify Columns?
The problem typically operates on a tabular structure such as a Pandas DataFrame. Conceptually, it behaves like a 2D array where each column can be selected and updated independently using vectorized operations.
What is the time complexity of Modify Columns?
Both common solutions run in O(n * m) time, where n is the number of rows and m is the number of columns being modified. Each cell in the targeted columns must be updated once. Space complexity is typically O(1) because updates can be applied in place.



