3. Distinct User Actions
Difficulty: Easy · Topics: Filtering & Selection
The logs table records every action a user performs, so the same (user_id, action) pair can appear many times.
Return each distinct (user_id, action) pair exactly once.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
logs
| user_id | action | ts |
|---|---|---|
| 1 | login | 2024-01-01 09:00 |
| 1 | login | 2024-01-02 09:00 |
| 1 | view | 2024-01-02 09:05 |
| 2 | login | 2024-01-01 10:00 |
| 2 | login | 2024-01-03 10:00 |
| 2 | purchase | 2024-01-03 10:30 |
Expected output
| user_id | action |
|---|---|
| 1 | login |
| 1 | view |
| 2 | login |
| 2 | purchase |
Hints
Hint 1
Select the two columns first, then call.distinct().Hint 2
dropDuplicates(["user_id", "action"]) does the same without a separate select, but keeps all columns.Learn the concepts
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
- dropDuplicates · 5 min read. Remove duplicate rows or keys, and why "keep the latest row" needs a window instead.
PySpark functions you'll practise
- select
- distinct
Related problems
- Filter High Earners · Easy · Filtering & Selection
- Build a Price List · Easy · Filtering & Selection
- Add a Bonus Column · Easy · Filtering & Selection
- Paid Orders in March · Easy · Filtering & Selection
- Sort Delivery Options · Easy · Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice