easy +8 pts

Rank Employees by Salary

Sort employee records by salary, then by name, and return formatted ranks.

You are given a list of employees. Each employee is a dictionary with keys "name" (string) and "salary" (integer). Write a function `rank_employees(employees)` that returns a list of strings formatted as `"{rank}: {name} - {salary}"`. Ranking rules: - Sort by salary in **descending** order. - If two employees have the same salary, sort their names in **ascending** alphabetical order. - Ranks are 1-based integers. Employees with equal salary share the **same rank**, and the next rank is incremented by the number of tied employees (competition ranking). For example, if two employees tie for rank 1, the following employee gets rank 3. The output list must be in the same order as the sorted employees.

Constraints

- 0 <= len(employees) <= 1000 - Each salary is an integer between 0 and 10^6. - Names are non-empty strings of lowercase letters, at most 50 characters, and unique. - Time complexity should be O(n log n) due to sorting.

Example

>>> rank_employees([{"name": "alice", "salary": 50000}, {"name": "bob", "salary": 60000}, {"name": "carol", "salary": 50000}])
['1: bob - 60000', '2: alice - 50000', '2: carol - 50000']
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Sort the employees using a key that prioritizes salary descending and name ascending.
Use a variable to track the current rank. When salary changes, update the rank to index + 1.
Format each string with f"{rank}: {name} - {salary}".
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.