Rank each person in a stream by the earlier arrivals stronger than them

Quick Overview

Given the strengths of people arriving one at a time, compute each person's rank as one plus the number of earlier arrivals who are strictly stronger. It tests counting order statistics over a growing set efficiently and handling ties correctly at up to 100,000 arrivals.

Rank each person in a stream by the earlier arrivals stronger than them

Company: Rubrik

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `strengths` where `strengths[i]` is the strength of the `i`-th person to arrive in a stream. When a person arrives, their rank is `1` plus the number of people who arrived earlier with a strength **strictly greater** than theirs. Earlier people with equal strength do not increase the arriving person's rank. Return an integer array `ranks` where `ranks[i]` is the rank of the `i`-th person at the moment they arrive. ### Function Signature ```python def person_rank(strengths: list[int]) -> list[int]: ``` ### Rules - Person `i` is compared only with persons `0` to `i - 1`. People who arrive later never change an earlier person's rank. - Ties do not count toward the rank. ### Constraints - `1 <= len(strengths) <= 10^5` - `1 <= strengths[i] <= 10^9` ### Examples ```text Input: strengths = [5,4,6,1] Output: [1,2,1,4] ``` 5 arrives first and gets rank 1. Only 5 is greater than 4, so 4 gets rank 2; nothing earlier is greater than 6, so it gets rank 1; and 5, 4 and 6 are all greater than 1, so it gets rank 4. ```text Input: strengths = [3,3,2,4,3] Output: [1,1,3,1,2] ``` The second 3 ties with the first, so both get rank 1. The 2 has two greater values before it (rank 3), the 4 has none (rank 1), and the last 3 has only the 4 above it (rank 2).

Overview: Given the strengths of people arriving one at a time, compute each person's rank as one plus the number of earlier arrivals who are strictly stronger. It tests counting order statistics over a growing set efficiently and handling ties correctly at up to 100,000 arrivals.

Rubrik logo
Rubrik
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array strengths where strengths[i] is the strength of the i-th person to arrive in a stream.

When a person arrives, their rank is 1 plus the number of people who arrived earlier with a strength strictly greater than theirs. Earlier people with equal strength do not increase the arriving person's rank.

Return an integer array ranks where ranks[i] is the rank of the i-th person at the moment they arrive.

Function Signature

def person_rank(strengths: list[int]) -> list[int]:

Rules

  • Person i is compared only with persons 0 to i - 1 . People who arrive later never change an earlier person's rank.
  • Ties do not count toward the rank.

Constraints

  • 1 <= len(strengths) <= 10^5
  • 1 <= strengths[i] <= 10^9

Examples

Input: strengths = [5,4,6,1]
Output: [1,2,1,4]

5 arrives first and gets rank 1. Only 5 is greater than 4, so 4 gets rank 2; nothing earlier is greater than 6, so it gets rank 1; and 5, 4 and 6 are all greater than 1, so it gets rank 4.

Input: strengths = [3,3,2,4,3]
Output: [1,1,3,1,2]

The second 3 ties with the first, so both get rank 1. The 2 has two greater values before it (rank 3), the 4 has none (rank 1), and the last 3 has only the 4 above it (rank 2).