DataFrame: employees
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| employee_id | int |
| name | object |
| department | object |
| salary | int |
+-------------+--------+
Write a solution to display the first 3 rows of this DataFrame.
Example 1:
Input: DataFrame employees +-------------+-----------+-----------------------+--------+ | employee_id | name | department | salary | +-------------+-----------+-----------------------+--------+ | 3 | Bob | Operations | 48675 | | 90 | Alice | Sales | 11096 | | 9 | Tatiana | Engineering | 33805 | | 60 | Annabelle | InformationTechnology | 37678 | | 49 | Jonathan | HumanResources | 23793 | | 43 | Khaled | Administration | 40454 | +-------------+-----------+-----------------------+--------+ Output: +-------------+---------+-------------+--------+ | employee_id | name | department | salary | +-------------+---------+-------------+--------+ | 3 | Bob | Operations | 48675 | | 90 | Alice | Sales | 11096 | | 9 | Tatiana | Engineering | 33805 | +-------------+---------+-------------+--------+ Explanation: Only the first 3 rows are displayed.
Problem Overview: You are given a DataFrame and need to return only the first three rows. The task focuses on selecting a small subset of rows from a tabular structure while preserving the original column layout.
Approach 1: Using Built-in DataFrame Methods (O(1) time, O(1) space)
Most DataFrame libraries provide direct utilities to retrieve the first k rows. In Python's pandas, df.head(3) returns the first three records without modifying the original DataFrame. JavaScript DataFrame libraries typically expose a similar slice or head-style function. Since the number of rows requested is constant (3), the operation runs in O(1) time and uses O(1) additional space for references to the rows. This approach relies on optimized library internals and is the cleanest way to solve the problem when working with structured tabular data.
Under the hood, the method simply selects the first three row indices and constructs a new view or shallow copy. No full traversal of the dataset is required. If you frequently work with tabular datasets, mastering built-in operations like head(), tail(), and slicing will significantly improve productivity.
Approach 2: Manual Iteration (O(k) time, O(k) space)
When built-in DataFrame utilities are unavailable, you can manually iterate through the dataset and collect the first three rows. Use a loop that stops after reading three records and store them in a result container such as a vector or list. Here k = 3, so the algorithm performs only three iterations, resulting in O(k) time and O(k) space for the output structure.
This approach is common in languages like C++ or Java where datasets are often represented as arrays, lists, or custom row objects. You iterate from index 0 up to min(3, n) and copy each row into the result. While simple, it demonstrates control over iteration and boundary handling.
The manual method is essentially a constrained traversal problem related to basic arrays and iteration. It also mirrors how row slicing works internally in many dataframe implementations.
Recommended for interviews: The built-in DataFrame method is the expected solution when the environment provides a DataFrame API. It is concise, readable, and leverages optimized library operations. Manual iteration still matters because it proves you understand how row selection works internally. Interviewers often expect you to recognize the library shortcut first, then explain the underlying iteration logic.
Most data manipulation libraries provide a direct method to access the top few rows of a DataFrame. This approach utilizes the built-in method to fetch the first three rows efficiently.
Using Pandas in Python, the head function allows us to grab the first few rows of the DataFrame. By default, it retrieves 5 rows, but you can specify the number, in this case, 3.
Python
JavaScript
Time Complexity: O(1), since it directly accesses the specified rows.
Space Complexity: O(1), no additional space usage beyond the output.
This approach involves manually iterating over the DataFrame and extracting the first three rows. It's useful in languages or environments where built-in functionality might not exist.
This C++ solution manually iterates through the rows of a DataFrame using a for loop. We ensure to only access rows as long as they exist, preventing out-of-bound errors.
Time Complexity: O(1), since we only loop through a fixed number of rows.
Space Complexity: O(1), as we only store a constant number of rows.
Python
| Approach | Complexity |
|---|---|
| Using Built-in Methods for DataFrames | Time Complexity: O(1), since it directly accesses the specified rows. |
| Manual Iteration | Time Complexity: O(1), since we only loop through a fixed number of rows. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-in DataFrame Methods (head/slice) | O(1) | O(1) | When working with pandas or DataFrame libraries that provide row-selection utilities |
| Manual Iteration | O(k) where k=3 | O(k) | When DataFrame helpers are unavailable and data is stored in arrays or lists |
LeetCode 2879 Display the First Three Rows in Python | Pandas Tutorial for Beginners • JR: Educational Channel • 1,258 views views
Watch 9 more video solutions →Practice Display the First Three Rows with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor