Calculating Average Building Height
Beginner Mode

Start your terminal to use beginner mode.

Objective

In this problem, you will be working with a DataFrame that represents a set of architectural structures. Each row in the dataset represents a unique building and includes various details about its architecture.

Task

Write a PySpark function that calculates the average height per floor for each building, with the result rounded to two decimal places.

If the number of floors is zero, the average height must gracefully default to zero to avoid mathematical errors. The result should include the id, name, city, country, and the calculated average height per floor (named avg_height_per_floor). Order the final output in ascending order by the avg_height_per_floor. If there is a tie, break it by sorting id in ascending order.

File Path

  • Buildings Dataset: /home/interview/buildings.csv
  • Starter script: /home/interview/building_heights.py

Schema

buildings.csv

Column Name Type
id Integer
name String
city String
country String
height_m Float
floors Integer

Expected Output Schema

Column Name Type
id Integer
name String
city String
country String
avg_height_per_floor Float

Example

Given this sample input:

buildings

| id | name | city | country | height_m | floors |
|----+---------------------------+----------+--------------+----------+--------|
| 1 | One World Trade Center | New York | USA | 541.3 | 104 |
| 2 | Willis Tower | Chicago | USA | 442.1 | 108 |
| 3 | Burj Khalifa | Dubai | UAE | 828.0 | 163 |
| 4 | The Shard | London | UK | 309.6 | 72 |
| 5 | Abraj Al-Bait | Mecca | Saudi Arabia | 601.0 | 120 |

The expected output would be:

| id | name | city | country | avg_height_per_floor |
|----+---------------------------+----------+--------------+----------------------|
| 2 | Willis Tower | Chicago | USA | 4.09 |
| 4 | The Shard | London | UK | 4.30 |
| 5 | Abraj Al-Bait | Mecca | Saudi Arabia | 5.01 |
| 3 | Burj Khalifa | Dubai | UAE | 5.08 |
| 1 | One World Trade Center | New York | USA | 5.20 |

Terminal requires a larger screen

Open this page on a desktop or tablet (≥ 768px) to launch the terminal and practice hands-on.

Linux Terminal Environment

Write and execute your solution in the terminal below.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →