Spark Review and Practice Session
1
CS224p
Prepared by Vedant Saraswat
Problem 1 : Grouping Common Event Status Together
Given the following data you are tasked to group the consecutive event states and display the start and end date for each group.
2
Event Date | Event Status |
2020-06-01 | Won |
2020-06-02 | Won |
2020-06-03 | Won |
2020-06-04 | Lost |
2020-06-05 | Lost |
2020-06-06 | Lost |
2020-06-07 | Won |
2020-06-08 | Won |
Expected Solution
3
Event Date | Event Status |
2020-06-01 | Won |
2020-06-02 | Won |
2020-06-03 | Won |
2020-06-04 | Lost |
2020-06-05 | Lost |
2020-06-06 | Lost |
2020-06-07 | Won |
2020-06-08 | Won |
Event Status | Start Date | End Date |
Won | 2020-06-01 | 2020-06-03 |
Lost | 2020-06-04 | 2020-06-06 |
Won | 2020-06-07 | 2020-06-08 |
PySpark Approach
4
5
6
7
8
SQL Approach
9
10
Problem 2 : Exchange Two Students in Class
Given the following data you are assigned the responsibility of having students exchange data with their alternate counterparts.
11
ID | Student |
1 | Alice |
2 | Bob |
3 | Charlie |
4 | David |
5 | Eve |
12
ID | Student |
1 | Alice |
2 | Bob |
3 | Charlie |
4 | David |
5 | Eve |
ID | Student |
1 | Bob |
2 | Alice |
3 | David |
4 | Charlie |
5 | Eve |
PySpark Approach
13
14
15
16
SQL Approach
17
18
Problem 3 : Number of Calls Between Two Persons
Given the provided data, your task is to group calls between the same individuals, calculating both the total duration and the count of the number of calls.
19
From_id | To_id | Duration |
10 | 20 | 58 |
20 | 10 | 12 |
10 | 30 | 20 |
30 | 40 | 100 |
30 | 40 | 200 |
30 | 40 | 200 |
40 | 30 | 500 |
Expected Solution
20
From_id | To_id | Duration |
10 | 20 | 58 |
20 | 10 | 12 |
10 | 30 | 20 |
30 | 40 | 100 |
30 | 40 | 200 |
30 | 40 | 200 |
40 | 30 | 500 |
Person 1 | Person 2 | total_duration | call_count |
10 | 20 | 70 | 2 |
10 | 30 | 20 | 1 |
30 | 40 | 1000 | 4 |
PySpark Approach
21
22
23
24
SQL Approach
25
26
Problem 4 : Counting Grand Slam Titles
Given the provided data, your task is to calculate the grand slam titles for each Players
27
Player_id | Player_name |
1 | Nadal |
2 | Federer |
3 | Novak |
Year | Wimbledon | Fr_open | US_open | Au_open |
2017 | 2 | 1 | 1 | 2 |
2018 | 3 | 1 | 3 | 2 |
2019 | 3 | 1 | 1 | 3 |
Expected Solution
28
Player_id | Player_name |
1 | Nadal |
2 | Federer |
3 | Novak |
Year | Wimbledon | Fr_open | US_open | Au_open |
2017 | 2 | 1 | 1 | 2 |
2018 | 3 | 1 | 3 | 2 |
2019 | 3 | 1 | 1 | 3 |
Player_id | Player_name | grand_slams |
1 | Nadal | 5 |
2 | Federer | 3 |
3 | Novak | 4 |
PySpark Approach
29
30
31
SQL Approach
32
33
Problem 5 : Employees Earning more than managers
Given the provided data, your task is to identify employees who are earning more than managers
34
ID | Name | Salary | ManagerId |
1 | John | 6000 | 4 |
2 | Kevin | 11000 | 4 |
3 | Bob | 8000 | 5 |
4 | Laura | 9000 | null |
5 | Sarah | 10000 | null |
Expected Solution
35
ID | Name | Salary | ManagerId |
1 | John | 6000 | 4 |
2 | Kevin | 11000 | 4 |
3 | Bob | 8000 | 5 |
4 | Laura | 9000 | null |
5 | Sarah | 10000 | null |
Name |
Kevin |
PySpark Approach
36
37
38
39
Problem 6 : Interview Employees Earning More Than Department Average Salary
Given the provided data, your task is to identify employees who are earning more than Department Average Salary
40
Employee ID | Employee Name | Department | Salary |
1 | Alice | HR | 60000 |
2 | Bob | HR | 50000 |
3 | Charlie | Finance | 70000 |
4 | David | Finance | 75000 |
5 | Eve | Engineering | 90000 |
6 | Frank | Engineering | 93000 |
7 | Grace | HR | 45000 |
8 | Hank | Engineering | 98000 |
9 | Ivy | Finance | 66000 |
Expected Solution
41
Employee ID | Employee Name | Department | Salary |
1 | Alice | HR | 60000 |
2 | Bob | HR | 50000 |
3 | Charlie | Finance | 70000 |
4 | David | Finance | 75000 |
5 | Eve | Engineering | 90000 |
6 | Frank | Engineering | 93000 |
7 | Grace | HR | 45000 |
8 | Hank | Engineering | 98000 |
9 | Ivy | Finance | 66000 |
Employee Name | Salary | Avg - Salary |
Hank | 98000 | 93666 |
David | 75000 | 70333 |
Alice | 60000 | 51666 |
PySpark Approach
42
43
44
SQL Approach
45
46