Amazon: Non-Managers Across Multiple Departments (PySpark)

PYSPARK coding challenge · Difficulty: medium · Topic: Joins · +100 XP

Problem

*Amazon-style interview practice.*

Topic: Joins · anti-join (NOT EXISTS / NOT IN / left anti join) · COUNT(DISTINCT ...) · GROUP BY / HAVING

Amazon's HR analytics team wants to find individual contributors with cross-team experience: employees who have never been a manager of anyone and who have worked in more than one department.

Using the PySpark DataFrame API (or Spark SQL), find those employees.

Assign the result to df_result with the columns employee_id and name, sorted by employee_id ascending, and finish with df_result.show().

Schema

DataFrame: employees

| Column | Type | Meaning |
| --- | --- | --- |
| employee_id | integer | The employee (repeats once per department assignment) |
| name | string | The employee's name |
| manager_id | integer | The `employee_id` of the manager in this assignment; null if none |
| department_id | integer | The department of this assignment |

Example 1

Input: employees

| employee_id | name | manager_id | department_id |
| --- | --- | --- | --- |
| 1 | Asha | null | 10 |
| 2 | Ravi | 1 | 10 |
| 2 | Ravi | 1 | 20 |
| 3 | Meera | 2 | 10 |
| 3 | Meera | 1 | 30 |
| 4 | Karan | 2 | 20 |
| 4 | Karan | 2 | 20 |
| 5 | Neha | 2 | 20 |
| 6 | Vikram | 1 | 30 |
| 6 | Vikram | 1 | 40 |
| 7 | Pooja | 6 | 40 |
| 8 | Imran | 6 | 30 |
| 8 | Imran | 2 | 20 |

Output:

`

+-----------+-----+

|employee_id| name|

+-----------+-----+

|          3|Meera|
|          8|Imran|

+-----------+-----+

`

Explanation:

Constraints

What this PYSPARK challenge teaches you

“Amazon: Non-Managers Across Multiple Departments (PySpark)” is a medium-level PYSPARK challenge focused on Joins. Working through it gives you hands-on practice with left_anti join, countDistinct, groupBy, Nulls, HR analytics — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Build the managers: employees.select(F.col('manager_id').alias('employee_id')).dropna().distinct().
  2. employees.join(managers, 'employee_id', 'left_anti') keeps the rows of people who manage nobody.
  3. groupBy('employee_id', 'name') with F.countDistinct('department_id'), then keep > 1.
  4. select('employee_id', 'name').orderBy('employee_id') before df_result.show().

Interview practice

This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYSPARK challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Joins.

Solve this challenge free on PySpark.in