Dense Ranks Preserving Value Order and Equality

Quick Overview

Dense Ranks Preserving Value Order and Equality. Replace each value by its smallest possible positive rank: equal values share a rank, and a larger value must have a larger rank.

Dense Ranks Preserving Value Order and Equality

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Replace each value by its smallest possible positive rank: equal values share a rank, and a larger value must have a larger rank. Return ranks in the original position order. The empty array is allowed. ```cpp class Solution { public: vector<int> arrayRankTransform(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [7,-2,7,0] Output: [3,1,3,2] Explanation: The distinct sorted values-2,0,7 receive ranks1,2,3. ``` **Example 2:** ```text Input: arr = [] Output: [] Explanation: There are no positions to rank. ``` ### Constraints - Array length is between 0 and 100000. - Each original value is an integer in `[-1000000000,1000000000]`. ### Contract considerations Keep the vector-reference callable; the reference replaces and returns that input array. ```hint Read the boundary condition Check equal values, negative values, and an empty input. Ranks must remain in the original position order and be as small as permitted. ``` ### Boundary reminders Keep the vector-reference callable; the reference replaces and returns that input array.

Overview: Dense Ranks Preserving Value Order and Equality. Replace each value by its smallest possible positive rank: equal values share a rank, and a larger value must have a larger rank.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Replace each value by its smallest possible positive rank: equal values share a rank, and a larger value must have a larger rank. Return ranks in the original position order. The empty array is allowed.

class Solution {
public:
    vector<int> arrayRankTransform(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [7,-2,7,0]
Output: [3,1,3,2]
Explanation: The distinct sorted values-2,0,7 receive ranks1,2,3.

Example 2:

Input: arr = []
Output: []
Explanation: There are no positions to rank.

Constraints

  • Array length is between 0 and 100000.
  • Each original value is an integer in [-1000000000,1000000000] .

Contract considerations

Keep the vector-reference callable; the reference replaces and returns that input array.

Boundary reminders

Keep the vector-reference callable; the reference replaces and returns that input array.