FastPrepSelect the Top User Matches

Select the Top User Matches

Duolingo logoDuolingo● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given parallel arrays userIds and scores, where each score was computed in constant time against one selected user. Return the IDs of the n best matches.

Order results by descending score, breaking ties by ascending user ID. n is much smaller than the number of candidates.

Function

topMatches(userIds: int[], scores: int[], n: int) → int[]

Examples

Example 1

userIds = [10,20,30,40]scores = [5,9,9,2]n = 2return = [20,30]

Users 20 and 30 tie for the highest score and are ordered by ID.

Example 2

userIds = [8,3,5]scores = [-1,4,2]n = 1return = [3]

User 3 has the highest score.

Constraints

  • 1 <= userIds.length == scores.length <= 1000000
  • User IDs are distinct signed 32-bit integers.
  • Scores are signed 32-bit integers.
  • 1 <= n <= userIds.length.

More Duolingo problems

See Duolingo hiring insights
public int[] topMatches(int[] userIds, int[] scores, int n) {
    // Write your code here.
}
userIds[10,20,30,40]
scores[5,9,9,2]
n2
expected[20,30]
Checking account…