The Relative Ranks problem gives you a list of athlete scores and asks you to assign ranks: 1st place → "Gold Medal", 2nd → "Silver Medal", 3rd → "Bronze Medal", others → their rank number as a string. This easy coding problem sorts with index tracking. The array, sorting, and heap interview pattern is demonstrated.
Electronic Arts, Amazon, Google, and Bloomberg ask this as a quick sorting with mapping problem. It validates sorted-index tracking and special-case rank labeling.
Sort with original index tracking. Create pairs (score, index). Sort descending by score. For each position in sorted order: assign the appropriate label ("Gold Medal", "Silver Medal", "Bronze Medal", or rank number string) to result[original_index].
score=[5,4,3,2,1]. Sorted: [(5,0),(4,1),(3,2),(2,3),(1,4)].
Relative Ranks demonstrates the "sort with index" pattern: enumerate, sort by value, assign to original positions. Always preserve indices when the output must match original array order. Practice this in: "assign grades," "rank products," "medal ceremony simulation." The special-case first 3 ranks require simple conditional handling.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Product of Two Elements in an Array | Easy | Solve | |
| Campus Bikes | Medium | Solve | |
| Choose K Elements With Maximum Sum | Medium | Solve | |
| Diagonal Traverse II | Medium | Solve | |
| Single-Threaded CPU | Medium | Solve |