Cracking the "Population Density" Question for Deloitte Data Science Interviews

Cracking the "Population Density" Question for Deloitte Data Science Interviews

Solve the "Population Density" data analysis question with Python. Master data manipulation, calculations, and insights for success in data science interviews.

Hello, I'm Saloni, a Senior Consultant and Data Scientist at Deloitte.

During one of Deloitte's recent data science interviews, a candidate was asked the below question related to population density.

This problem tests your ability to work with structured data, perform calculations, and identify key insights. ✅

It's a fundamental data analysis question that assesses skills in data manipulation, mathematical reasoning, and effective communication of results.

Problem Statement

You are working on a data analysis project at Deloitte where you need to analyse a dataset containing information about various cities.

Your task is to calculate the population density of these cities, rounded to the nearest integer, and identify the cities with the minimum and maximum densities.

The population density should be calculated as (Population / Area).

The output should contain 'city', 'country', 'density'.

Data

Solution:

Approach:

📌 Define the dataset as a list of lists, where each inner list represents a city with the format [city, country, population, area].

📌  Create a function calculate_population_density that takes the population and area as input and calculates the population density as population / area.

If the area is 0, it should return "Undefined". Otherwise, it should round the density to the nearest integer using the round function.

📌  Initialise an empty list cities_with_densities to store the cities with their densities, and variables min_density and max_density to keep track of the minimum and maximum densities, respectively.

📌  Iterate through the dataset, and for each city:

Calculate the population density using the calculate_population_density function.

If the density is not "Undefined", append a tuple (city, country, density) to the cities_with_densities list and update min_density and max_density if necessary.

📌  Print the header "City, Country, Density".

📌  Iterate through the cities_with_densities list and print the citycountry, and density for each city.

📌  Print the minimum and maximum densities.

Python Implementation:

import math

# Define the dataset
data = [
    ["Metropolis", "Countryland", 1000000, 500],
    ["Smallville", "Countryland", 50000, 1000],
    ["Coastcity", "Oceanland", 300000, 0],
    ["Starcity", "Mountainous", 600000, 600],
    ["Gotham", "Islander", 1500000, 300],
    ["Rivertown", "Plainsland", 100000, 5000],
    ["Lakecity", "Forestland", 100000, 5000],
    ["Hilltown", "Hillside", 200000, 450],
    ["Forestville", "Forestland", 500000, 700],
    ["Oceanview", "Seaside", 800000, 0]
]

# Function to calculate population density
def calculate_population_density(population, area):
    if area == 0:
        return "Undefined"
    else:
        density = population / area
        return round(density)

# Process the data and find minimum and maximum densities
cities_with_densities = []
min_density = float('inf')
max_density = 0

for city, country, population, area in data:
    density = calculate_population_density(population, area)
    if density != "Undefined":
        cities_with_densities.append((city, country, density))
        min_density = min(min_density, density)
        max_density = max(max_density, density)

# Print the output
print("City, Country, Density")
for city, country, density in cities_with_densities:
    print(f"{city}, {country}, {density}")

print(f"\nMinimum density: {min_density}")
print(f"Maximum density: {max_density}")


***

If you found this problem interesting and want to practice more such data analysis questions, feel free to connect with me for a free 1:1 call.

I'd be happy to discuss more interview preparation strategies and help you enhance your problem-solving skills for data science roles.