Top 2 Earners per Department (Ties Included)

PySpark practice question · Medium · Window Functions

Return the employees whose salary is among the top 2 salaries in their department using rank(): an employee is included when their rank (highest salary = 1, ties share a rank, gaps after ties) is 1 or 2. Ignore employees with a NULL salary.

Example 1

Input employees
emp_idnamedeptsalarymanager_idhire_date
1AaravSales50000NULL2022-01-01
2DiyaSales6000012022-01-01
3KabirSales4500012022-01-01
4MeeraIT90000NULL2022-01-01
5RohanIT8000042022-01-01
6SnehaIT7000042022-01-01
Expected output
deptnamesalary
ITMeera90000
ITRohan80000
SalesDiya60000
SalesAarav50000

Example 2

Input employees
emp_idnamedeptsalarymanager_idhire_date
1IshaHR52000NULL2022-01-01
2VikramHR5200012022-01-01
3NehaHR3800012022-01-01
Expected output
deptnamesalary
HRIsha52000
HRVikram52000

Constraints

Return columns dept, name, salary. Row order does not matter.

Solve it in the live editor on this page — run on sample data, then submit against hidden tests.

‹ Previous Order Amount with lag() · Day-over-Day Revenue Change ›

Top 2 Earners per Department (Ties Included) – PySpark Interview Question | TechTown
T
Tech Town
Hi, {{ authName }} · Dashboard Log Out Log In Sign Up Free
PySpark Interview Practice

Practice PySpark Online

{{ totalCount }} questions · {{ shHardPct }}% Hard. Real PySpark DataFrame and Spark SQL interview questions. Write PySpark in your browser on TechTown’s Spark engine, run sample tests, then submit against hidden tests.

{{ totalCount }}
Questions
{{ shTopicCount }}
Topics
{{ solvedCount }}
Solved
{{ accuracy }}
Acceptance
Bookmark Question Topic Difficulty Status
{{ r.n }} - {{ r.title }}{{ r.badge }}{{ r.lock }} {{ r.topic }} {{ r.difficulty }} {{ r.statusIcon }}
No questions match these filters.
Showing {{ shRange }} of {{ shFilteredCount }}

Guests can solve {{ freeLimit }} problems for free. Log in or sign up (free) to unlock everything, keep your streak and appear on the leaderboard. New to a topic? Learn it first in the Spark course.

{{ p.num }}. {{ p.title }} {{ p.difficulty }} {{ p.topic }} {{ p.solvedLabel }}
{{ shPointsLabel }} ⏱ {{ shTimer }}
Embed this questionPaste on a blog, course page or LMS. Free to use — educator guide
{{ shEmbedCode }}

{{ p.statement }}

Example {{ ex.n }}
{{ tb.title }}
{{ c.v }}
Expected result
{{ c.v }}
Constraints

{{ p.constraints }}

  1. {{ h }}
Learn the concept first: {{ p.topic }} in the Spark course
Your submissions (last 20, saved on this device)

No submissions yet. Press Submit to judge your code against all tests.

{{ sb.verdict }}
{{ sb.meta }}
🔒 Solution locked

The model solution and step-by-step explanation unlock when you solve this problem or after 3 unsuccessful submissions ({{ shFailsLabel }}). Struggling? Try the hints on the Question tab first.

Why this approach — step by step
  1. {{ st }}
Optimal solution (PySpark)
{{ p.solution }}
Runs in an isolated browser sandbox · {{ engineLabel }}

Write real PySpark: from pyspark.sql import functions as F, Window, spark.sql(...). It runs in your browser on a lightweight Spark engine, and the same code runs unchanged on Databricks or any Spark cluster. Use df.show() or print() to debug.

{{ verdict }} {{ verdictDetail }}
{{ errorText }}
{{ tr.label }} {{ tr.msg }}
Expected
{{ c.v }}
Your output
{{ c.v }}
stdout:
{{ stdout }}

Enjoying your practice journey?

Create your free TechTown account to unlock every problem, track your progress, bookmark questions and keep preparing for interviews.

Log in / Sign up