Sure, here's an example coding exam for data engineers:
Question 1: Data Transformation
Given a CSV file with the following columns:
customer_id: unique identifier for each customer
date: the date when the customer made a purchase
product: the product that the customer purchased
price: the price of the product
Write a Python function that takes in the path to the CSV file as an argument, reads the file, and returns a new CSV file with the following columns:
customer_id: unique identifier for each customer
total_spent: the total amount of money that the customer has spent on all purchases
Question 2: Data Pipeline
Assume you have two data sources that you want to combine into a single data source. One data source is a CSV file with the following columns:
customer_id: unique identifier for each customer
name: the customer's name
age: the customer's age
The other data source is a PostgreSQL database with a table that has the following columns:
customer_id: unique identifier for each customer
last_purchase_date: the date when the customer made their most recent purchase
Write a Python script that performs the following tasks:
Reads the CSV file and the PostgreSQL database table
Joins the data from the two sources on the customer_id column
Filters out customers who are under the age of 18
Writes the resulting data to a new table in the same PostgreSQL database
Question 3: Data Modeling
Assume you are working on a project to build a data warehouse for an e-commerce website. The website has the following tables:
customers: contains information about each customer, such as their name, email address, and phone number
orders: contains information about each order, such as the date it was placed, the customer who placed the order, and the total price of the order
products: contains information about each product, such as its name, description, and price
Write a SQL script that creates a star schema for the e-commerce website. The star schema should have the following tables:
fact_orders: contains aggregated information about each order, such as the date it was placed, the customer who placed the order, the total price of the order, and the number of items in the order
dim_customers: contains detailed information about each customer, such as their name, email address, and phone number
dim_products: contains detailed information about each product, such as its name, description, and price
The fact_orders table should have foreign keys to the dim_customers and dim_products tables, as well as a foreign key to a date dimension table. The fact_orders table should be aggregated at the day level. The dim_customers and dim_products tables should have primary keys on their respective id columns.
As an AI language model, I cannot write answers to coding problems without access to the coding environment or the prompt inputs. However, here are some general guidelines on how to approach each problem:
Question 1: Data Transformation
To solve this problem, you can use the pandas library in Python to read in the CSV file as a DataFrame, group the data by customer_id, and sum the prices to get the total_spent column. Then, use the to_csv() method to write the transformed data to a new CSV file.
Here's an example solution:
python
import pandas as pd
def transform_data(csv_path):
data = pd.read_csv(csv_path)
grouped_data = data.groupby('customer_id')['price'].sum().reset_index()
grouped_data.columns = ['customer_id', 'total_spent']
grouped_data.to_csv('transformed_data.csv', index=False)
Question 2: Data Pipeline
To solve this problem, you can use the pandas library to read in the CSV file as a DataFrame, and the psycopg2 library to read in the data from the PostgreSQL database as a DataFrame. Then, you can use merge() method to join the two DataFrames on the customer_id column, filter the resulting DataFrame for customers over 18 years old, and use to_sql() method to write the filtered data to a new table in the PostgreSQL database.
Here's an example solution:
python
import pandas as pd
import psycopg2
def data_pipeline(csv_path, db_params):
# Read CSV data
csv_data = pd.read_csv(csv_path)
# Read PostgreSQL data
conn = psycopg2.connect(**db_params)
pg_data = pd.read_sql_query("SELECT customer_id, last_purchase_date FROM table_name", conn)
conn.close()
# Join and filter data
merged_data = pd.merge(csv_data, pg_data, on='customer_id')
filtered_data = merged_data[merged_data['age'] >= 18]
# Write filtered data to PostgreSQL
conn = psycopg2.connect(**db_params)
filtered_data.to_sql('new_table_name', conn, index=False)
conn.close()
Question 3: Data Modeling
To solve this problem, you can write a SQL script that creates the fact and dimension tables according to the star schema. The fact table should be created using an aggregated query that calculates the total price of each order, the number of items in each order, and groups the data by the date, customer, and product. The dimension tables should be created using simple CREATE TABLE statements that define the columns and data types.
Here's an example solution:
sql
-- Create the fact_orders table
CREATE TABLE fact_orders (
order_date DATE REFERENCES date_dim(date),
customer_id INTEGER REFERENCES dim_customers(id),
product_id INTEGER REFERENCES dim_products(id),
total_price DECIMAL,
num_items INTEGER,
PRIMARY KEY (order_date, customer_id, product_id)
);
INSERT INTO fact_orders (order_date, customer_id, product_id, total_price, num_items)
SELECT
DATE_TRUNC('day', o.date) AS order_date,
c.id AS customer_id,
p.id AS product_id,
SUM(o.price) AS total_price,
COUNT(o.product) AS num_items
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN products p ON o.product = p.name
GROUP BY order_date, customer_id, product_id;
-- Create the dim_customers table
CREATE TABLE dim_customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(20