Skip to main content

H-Index in Coding Interviews: Sorting vs Counting Buckets

Page 1

H-Index in Coding Interviews: Sorting vs Counting Buckets The H-Index problem looks like a statistics question, but interviewers use it to see whether you can turn a definition into an invariant. A researcher has index h if h of their papers have at least h citations each, and the remaining papers have no more than h. Given an array of citation counts, return the largest such h.

Step 1: restate the definition as a check For a candidate value h, count how many papers have at least h citations. If that count is at least h, the candidate works. The answer is the largest working h, and it can never exceed n, the number of papers. Saying this out loud early shows the interviewer you have bounded the search space to 0..n.

Solution 1: sort descending, O(n log n) Sort citations from largest to smallest. Walk the list with index i (0-based). While citations[i] >= i + 1, the first i + 1 papers all have at least i + 1 citations, so h can be i + 1. Stop at the first position where that fails. def h_index(citations): citations.sort(reverse=True) h = 0 for i, c in enumerate(citations): if c >= i + 1: h = i + 1 else: break return h

This is easy to explain and easy to test: [3, 0, 6, 1, 5] sorts to [6, 5, 3, 1, 0], and the loop stops at index 3, giving h = 3.

Solution 2: counting buckets, O(n) Because the answer is capped at n, any citation count above n can be clamped to n. Build buckets of size n + 1, where bucket[k] counts papers with exactly k citations (and bucket[n] holds everything at n or more). Then scan k from n down to 0, keeping a running total of papers with at least k citations. The first k where the total reaches k is the answer. def h_index(citations): n = len(citations) bucket = [0] * (n + 1) for c in citations: bucket[min(c, n)] += 1 total = 0 for k in range(n, -1, -1): total += bucket[k] if total >= k: return k return 0

Follow-up: H-Index II (sorted input) If the array is already sorted ascending, binary search for the leftmost index i where citations[i] >= n - i.


The answer is n - i. This is a nice place to mention the "binary search on a monotone predicate" pattern, because the predicate flips from false to true exactly once.

Edge cases to say out loud • All zeros: the answer is 0. • One paper with many citations: the answer is 1, not the citation count. • Every paper above n: the answer is n, which is why clamping works.

How to present it Start with the sorting solution, prove the stopping rule in one sentence, then offer the bucket version as the optimization once the interviewer asks about time complexity. If you want to rehearse this kind of explanation under time pressure, AI help for online coding assessments can be useful for practice sessions. Disclosure: this guide was written by the Linkjob AI team.


Turn static files into dynamic content formats.

Create a flipbook