PracticeArrays

Level-Up Rank

Easy/MediumArraystraversalstate trackingcomparison Time not estimated Not started

Context

Two rival players earn score rewards from separate level runs. Each player's scores are already listed from smallest to largest. The game wants to identify the score occupying a chosen rank after treating both score lists as one ordered collection. Equal scores still occupy separate positions, so the result depends on counting every score.

Problem

Implement find_level_rank. The function receives two nondecreasing arrays of scores and a valid one-based rank. Imagine merging the arrays into one nondecreasing sequence while keeping duplicate scores as separate entries. Return the score at the requested rank. Advance through the arrays with two positions, selecting the smaller available score each time. If the current scores are equal, select from the first array, though either equal value produces the same result. When one array is exhausted, continue counting from the other array. Do not sort or otherwise rearrange the input arrays.

Examples

Example 1
Input: first_scores = [10, 20, 30]rank = 4second_scores = [15, 25, 35]
Output: 25
Explanation: For example 1, the executed output is 25.
Example 2
Input: first_scores = [2, 2, 5, 9]rank = 3second_scores = [2, 4, 4]
Output: 2
Explanation: For example 2, the executed output is 2.
Example 3
Input: first_scores = [-8, -3, 1]rank = 5second_scores = [6, 12]
Output: 12
Explanation: For example 3, the executed output is 12.

Constraints

  • 0 <= len(first_scores) <= 100
  • -100000 <= first_scores[i] <= 100000 for every i
  • first_scores: sorted non decreasing
  • 0 <= len(second_scores) <= 100
  • -100000 <= second_scores[i] <= 100000 for every i
  • second_scores: sorted non decreasing
  • 1 <= rank <= 200
  • Types: first_scores is int[], second_scores is int[], rank is int; result is int

Function signature

def find_level_rank(first_scores: list[int], second_scores: list[int], rank: int) -> int
first_scores list[int]
Scores from the first level run, in nondecreasing order.
second_scores list[int]
Scores from the second level run, in nondecreasing order.
rank int
The one-based position requested in the combined ordering.
returns int
The score occupying the requested one-based rank after the two sorted score arrays are merged, with duplicate values counted separately.

Adapted from MBPP problem task_597 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • The inputs are valid when the requested rank does not exceed their combined length.
  • Every occurrence counts separately, including equal scores within one array or across both arrays.
Approved · c024-abs_461_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1