H-Index solution explained

To compute an h-index, sort the citation counts from highest to lowest and find the last position i (counting from 1) where the paper still has at least i citations; that i is the answer. Sorting gives O(n log n). A linear answer puts the counts in n + 1 buckets, with anything above n capped at n, then walks down from n adding papers until the running total reaches the bucket index.

By Published 6 min read

The task, restated

A researcher has n papers, and you get a list with the number of citations each one received. Their h-index is the largest whole number h for which at least h of the papers have been cited h or more times each. If no paper has a citation, h is 0. The answer is a single number between 0 and n.

Examples made up for this walkthrough
CitationshWhy
[5, 0, 2, 8, 3]3Three papers (8, 5, 3) have at least 3; there are not four with at least 4
[10, 10, 10]3Capped by the number of papers
[4, 4, 0, 0]2Two papers with at least 2, but not three with at least 3
[0, 0]0No paper has a citation
[1]1One paper with at least one citation
Examples made up for this walkthrough. Scroll sideways on a small screen.

There is a sibling problem where the list arrives already sorted in ascending order. Then binary search finds h in O(log n). Ask which version you have.

What the interviewer is checking

The definition is easy to misread, so the first test is whether you restate it correctly and check it on an example. After that, interviewers look for the sorted scan and, as a follow-up, the counting version that uses the cap at n. Explaining why counts above n can be merged is the key insight they want to hear.

Brute force: try each h

def h_index_brute(citations):
    n = len(citations)
    for h in range(n, -1, -1):
        if sum(1 for c in citations if c >= h) >= h:
            return h
    return 0
Try h from n down to 0 and count qualifying papers, O(n²).

It returns the first h that works when counting down, which is the largest. h = 0 always works, so the loop always returns.

Better: sort from highest to lowest

After sorting in descending order, the paper at position i (counting from 1) is the i-th most cited. If it has at least i citations, then the top i papers all have at least i, so h is at least i. As soon as a paper has fewer citations than its position, no larger h can work, because every later paper has even fewer.

def h_index_sorted(citations):
    ranked = sorted(citations, reverse=True)
    h = 0
    for position, cites in enumerate(ranked, start=1):
        if cites >= position:
            h = position
        else:
            break
    return h
Sorted scan, O(n log n) time.

Linear: buckets capped at n

Since h is at most n, a paper with 50 citations and one with n citations play the same role. So make n + 1 buckets, where bucket k counts papers with exactly k citations, and bucket n also collects every paper with more than n. Then walk from k = n down to 0, keeping a running total of papers with at least k citations. The first k where that total is at least k is the answer.

def h_index(citations):
    n = len(citations)
    buckets = [0] * (n + 1)
    for c in citations:
        buckets[min(c, n)] += 1
    at_least = 0
    for k in range(n, -1, -1):
        at_least += buckets[k]
        if at_least >= k:
            return k
    return 0
Python: counting buckets, O(n) time, O(n) space.
#include <algorithm>
#include <vector>

int hIndex(const std::vector<int>& citations) {
    int n = static_cast<int>(citations.size());
    std::vector<int> buckets(n + 1, 0);
    for (int c : citations) buckets[std::min(c, n)]++;
    int atLeast = 0;
    for (int k = n; k >= 0; --k) {
        atLeast += buckets[k];
        if (atLeast >= k) return k;
    }
    return 0;
}
C++: the same buckets.

Trace [5, 0, 2, 8, 3] with n = 5: 5 and 8 both land in bucket 5, the others in buckets 0, 2 and 3. Walking down: at k = 5 the total is 2, at k = 4 still 2, at k = 3 it becomes 3, and 3 is at least 3, so h is 3.

Cost of each approach

n is the number of papers
ApproachTimeExtra space
Try each hO(n²)O(1)
Sort descending, scanO(n log n)O(n) for the sorted copy, or O(1) sorting in place
Buckets capped at nO(n)O(n)
Already sorted ascending, binary searchO(log n)O(1)
n is the number of papers. Scroll sideways on a small screen.

Edge cases to test

  • All zeros: h is 0.
  • An empty list, if allowed: 0.
  • Every paper with a huge count: h equals n.
  • A single paper with 0 citations versus 1 citation.
  • Ties around the boundary, such as [3, 3, 3, 1], where h is 3.

Common mistakes

  • Returning the citation count of a paper rather than a position, which can exceed the number of papers.
  • Sorting ascending but scanning as if it were descending.
  • Forgetting to cap large counts in the bucket method, which indexes past the end of the array.
  • Off-by-one positions: the first paper is position 1, not 0.
  • Treating "at least h papers" as "exactly h papers".

How to explain it out loud

Fictional example

From definition to linear time
Candidate
Let me restate it: h is the biggest number where at least h papers have h or more citations. On 5, 0, 2, 8, 3 that is 3.
Candidate
If I sort from highest to lowest, the i-th paper having at least i citations means h is at least i, and the first time that fails I can stop. That is n log n.
Candidate
For linear time, h can never be more than n, so I bucket counts with everything above n in the top bucket, then walk down adding papers until the total reaches the bucket number.

Candy is another problem where a careful reading of the rule matters more than any clever structure. Majority Element II also turns a counting question into a small amount of state. Top K Frequent Elements uses the same bucket-by-count idea, and Binary Search covers the boundary search for the presorted version. See the coding problems hub for every walkthrough.

Natively for practice rounds

Problems with a tricky definition are good for practicing the restate-then-check habit. In a mock session on Mac or Windows, Natively transcribes your explanation so you can see whether you checked the definition on an example before coding. It can also read a problem from a screenshot and draft an approach, which is handy for reviewing your own answer once you finish. The AI assistant for coding interviews page explains how.

Questions people ask

How do you calculate the h-index from a list of citations?

Sort the counts from highest to lowest. The h-index is the last position i, counting from 1, where the paper at that position has at least i citations.

How do you solve H-Index in O(n)?

Use n + 1 buckets, putting any count above n into bucket n. Walk from n down to 0 with a running total of papers; the first k where the total is at least k is the answer.

Why can citation counts above n be capped?

The h-index can never exceed the number of papers, so a paper with more than n citations counts the same as one with exactly n.

What if the citations are already sorted?

Then binary search finds the answer in O(log n), by looking for the first position where the count is at least the number of papers from there to the end.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP