Quick Overview

Find the largest k for which at least k instance ratings are at least k, handling threshold equality and the zero-answer case.

Find the Largest Rating Threshold Supported by K Instances

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

Given an array of integer instance ratings, return the largest integer `k` such that at least `k` instances have ratings greater than or equal to `k`. ### Input and Output - Input: an array of integer ratings, one value per instance. - Output: one integer, the maximum qualifying `k`. Return `0` when no positive `k` qualifies. - `k` cannot exceed the number of instances. A rating equal to `k` meets the threshold. - For this practice version, an empty array has answer `0`. ### Example 1 ```text ratings = [3, 0, 6, 1, 5] answer = 3 ``` Three ratings are at least `3`: `3`, `6`, and `5`. ### Example 2 ```text ratings = [0, 0] answer = 0 ```

Overview: Find the largest k for which at least k instance ratings are at least k, handling threshold equality and the zero-answer case.

Given an array of integer instance ratings, return the largest integer `k` such that at least `k` instances have ratings greater than or equal to `k`. ### Input and Output - Input: an array of integer ratings, one value per instance. - Output: one integer, the maximum qualifying `k`. Return `0` when no positive `k` qualifies. - `k` cannot exceed the number of instances. A rating equal to `k` meets the threshold. - For this practice version, an empty array has answer `0`. ### Example 1 ```text ratings = [3, 0, 6, 1, 5] answer = 3 ``` Three ratings are at least `3`: `3`, `6`, and `5`. ### Example 2 ```text ratings = [0, 0] answer = 0 ```

Constraints

  • ratings is a finite array of integers and may be empty.
  • No rating-magnitude or array-size bound is supplied.
  • Return the largest k between 0 and the number of instances with at least k ratings greater than or equal to k.

Examples

Input: ([3, 0, 6, 1, 5],)

Expected Output: 3

Explanation: Three ratings reach at least three, but fewer than four reach four.

Input: ([0, 0],)

Expected Output: 0

Explanation: No positive threshold has enough qualifying instances.

Hints

  1. A threshold cannot exceed the number of instances.
  2. A rating equal to the threshold counts toward it.

Loading coding console...

Show the approach

Approach

If there are n ratings, no valid threshold exceeds n. Count how many ratings fall at each positive threshold from 1 through n, placing every rating at least n in the n bucket; nonpositive ratings contribute to none. Scan thresholds from n down to 1 while accumulating the number of ratings at least that threshold. The first threshold whose accumulated count is at least the threshold is the maximum qualifying k. Equality is included by the bucket count. Empty input has no positive threshold.

Time complexity:
O(n), where n is the number of ratings.
Space complexity:
O(n) for threshold buckets.