Quick Overview

Return every integer in descending ranked order while preserving duplicates. This final coding follow-up builds on earlier maximum-of-two and maximum-of-list warm-ups.

Return Integers in Descending Ranked Order

Company: Yelp

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Implement: ```text rank_descending(values: List[int]) -> List[int] ``` Return all input integers in nonincreasing order. Standard sorting routines are allowed for this final ranked-list task. ### Constraints - `0 <= len(values) <= 200_000` - `-10^9 <= values[i] <= 10^9` - Duplicate values must be preserved. ### Clarifications - “Ranked order list” means the values themselves ordered from largest to smallest, not ordinal rank labels. - An empty input returns an empty list. - Stability among equal integers is not observable and is not required. - The earlier interview warm-ups asked for the larger of two integers and then the largest value in a list without a built-in maximum. This callable is only the final ranked-list follow-up. ```hint Order every value Use a descending comparison, or sort in ascending order and reverse the result. ``` ### Examples ```text Input: values = [4, 1, 4, -2, 3] Output: [4, 4, 3, 1, -2] Input: values = [] Output: [] ``` ### Evaluation Focus - Correct handling of duplicates, negatives, empty lists, and one-element lists. - Exact nonincreasing output containing every input occurrence once. ### Extension How would you return dense rank labels, where equal values share a rank and the next distinct value receives the next integer rank?

Overview: Return every integer in descending ranked order while preserving duplicates. This final coding follow-up builds on earlier maximum-of-two and maximum-of-list warm-ups.

Read the full Yelp Data Scientist interview experience this question came from

Given a list of integers, return all of its values in nonincreasing order. Preserve duplicate occurrences. The input may be empty, and using a sorting routine is allowed.

Constraints

  • 0 <= values.length <= 200000
  • -1000000000 <= values[i] <= 1000000000
  • Duplicate values must be preserved
  • Return values in nonincreasing order

Examples

Input: ([4, 1, 4, -2, 3],)

Expected Output: [4, 4, 3, 1, -2]

Explanation: The source example is ranked from greatest to least while preserving both copies of 4.

Input: ([],)

Expected Output: []

Explanation: An empty input has an empty descending ranking.

Hints

  1. Use a sorting operation configured for descending numeric order.
  2. Do not remove duplicate occurrences.

Loading coding console...

Show the approach

Approach

Apply a comparison sort with descending order and return the resulting list. A comparison sort orders every pair of positions consistently, so each result element is greater than or equal to the element after it. Sorting reorders occurrences rather than collapsing them, which preserves duplicates. An empty list and a singleton are already valid orders and are handled directly by the same operation. The reference implementations copy the input before sorting so the returned ranking does not require callers to accept mutation of their list.

Time complexity:
O(n log n) in the general comparison-sorting case.
Space complexity:
O(n) for the returned copy; the sorting routine may use additional implementation-dependent workspace.