Get the Size of a DataFrame - Problem

Given a DataFrame called players with the following schema:

Column NameType
player_idint
nameobject
ageint
positionobject
......

Write a solution to calculate and display the number of rows and columns of the DataFrame.

Return the result as an array: [number of rows, number of columns]

Input & Output

Example 1 — Basic DataFrame
$ Input: players = [{"player_id": 1, "name": "Alice", "age": 25, "position": "Forward"}, {"player_id": 2, "name": "Bob", "age": 30, "position": "Guard"}, {"player_id": 3, "name": "Charlie", "age": 28, "position": "Center"}]
Output: [3, 4]
💡 Note: The DataFrame has 3 rows (players) and 4 columns (player_id, name, age, position), so we return [3, 4]
Example 2 — Single Row
$ Input: players = [{"player_id": 1, "name": "Alice", "age": 25, "position": "Forward"}]
Output: [1, 4]
💡 Note: Only one player with 4 attributes, so dimensions are [1, 4]
Example 3 — Empty DataFrame
$ Input: players = []
Output: [0, 0]
💡 Note: Empty DataFrame has no rows and no columns, returning [0, 0]

Constraints

  • DataFrame can have 0 or more rows
  • DataFrame can have 0 or more columns
  • All rows must have the same number of columns
  • Column names are strings
  • Cell values can be any data type

Visualization

Tap to expand
DataFrame Size CalculationDataFrame: playersplayer_idnameageposition1Alice25Forward2Bob30Guard3Charlie28Center3 Rows4 Columnsplayers.shapeReturns (3, 4)Output: [3, 4]
Understanding the Visualization
1
Input DataFrame
DataFrame with player data in rows and columns
2
Get Dimensions
Extract number of rows and columns
3
Output Array
Return [rows, columns] as result
Key Takeaway
🎯 Key Insight: Use DataFrame.shape property to get dimensions instantly in O(1) time
Asked in
Meta 15 Netflix 12 Spotify 8
33.0K Views
High Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen