Watch 10 video solutions for Display the First Three Rows, a easy level problem. This walkthrough by JR: Educational Channel has 1,258 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |