InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Arrays & Hashing

H-Index

medium Original ↗ 00:00

Problem

You are given an array citations where citations[i] is the number of citations a researcher’s i-th paper received. Compute the researcher’s h-index: the largest number h such that the researcher has at least h papers with at least h citations each.

Equivalently: find the largest h for which “h papers each have h or more citations” holds. h can never exceed the number of papers.

Examples

Example 1: citations = [3,0,6,1,5]3 Three papers (with 3, 6, 5 citations) each have ≥ 3 citations. There are not four papers with ≥ 4 citations each (only 6 and 5 qualify), so h = 3.

Example 2: citations = [1,3,1]1 Only one paper has ≥ 2 citations, so h = 2 fails. All three papers have ≥ 1 citation, so h = 1 holds.

Example 3: citations = [100]1 With a single paper, h cannot exceed 1.

Constraints

  • n == citations.length, 1 <= n <= 5000
  • 0 <= citations[i] <= 1000

n ≤ 5000 lets an O(n²) scan pass, but the intended answers are O(n log n) via sorting or O(n) via counting — and the O(n) one exploits that h can never exceed n.

Think about it first

Hint 1 For a fixed candidate value h, how do you check in one pass whether the h-index is at least h?
Hint 2 Sort the citations in descending order. Reading left to right, position i (0-based) tells you "there are i+1 papers with at least citations[i] citations". When does the h condition hold?
Hint 3 Any citation count above n is no more useful than exactly n. Bucket-count papers by min(citations, n), then sweep h from n down to 0, accumulating how many papers have ≥ h citations; the first h where that running total reaches h is the answer.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug