← Back
00:00

Rank Elements by Priority

Agoda interview questions · Easy · Coding round · 10 tests

Given an array of priority values, assign each element a rank such that the largest value gets rank 1, the second-largest distinct value gets rank 2, and so on. Elements with equal priority get the same rank.

Interface

rankByPriority(priorities: number[]): number[]

The part that trips people up

This is dense ranking, not the "Olympic"/competition ranking you may already have memorized. Ties do not consume extra rank numbers - the next distinct value always gets the next integer rank, regardless of how many elements tied for the rank before it.

Idle