H-Index II solution explained
H-Index II is a binary search for the first position i in the ascending citation list where citations[i] is at least n - i, the number of papers from i to the end. The answer is n minus that position, found in O(log n) time and O(1) space. A linear scan gives the same answer in O(n) and is the natural first step.
By Evin JohnPublished 7 min read
The problem in plain words
A researcher has n papers, and you get the number of citations each one has received, already sorted in non-decreasing order. Their h-index is the largest number h such that h of the papers have each been cited at least h times. Return h. The twist compared with the first version of this problem is the sorted input and the expectation that you use it: the interviewer wants logarithmic time.
| Citations (sorted) | h-index | Reason |
|---|---|---|
| [0, 2, 3, 3, 8] | 3 | Three papers (3, 3, 8) have at least 3 citations; four papers do not all reach 4 |
| [1, 1, 1] | 1 | Every paper has 1, so h = 1, but not all reach 2 |
| [0, 0] | 0 | No paper is cited at all |
| [10, 20, 30] | 3 | h cannot exceed the number of papers |
| [6] | 1 | One paper with plenty of citations |
Questions to settle first: can the list be empty (then h is 0), can counts be zero (yes, often), and is the order guaranteed ascending rather than descending? If it were descending, the same idea works with the comparison mirrored.
Why it is asked
This is a binary search on an answer boundary rather than on a value, and that is a skill many candidates have not practiced. There is no target number to look up. Instead you define a yes or no question about each index, show that the answers go no, no, no, yes, yes as you move right, and search for the first yes. Interviewers like it because the definition of h is easy to misread, and a candidate who restates it with an example before coding usually avoids the off-by-one traps.
Turning the definition into a condition on an index
Pick any index i. Because the list is sorted, the papers at positions i through n - 1 are the n - i most cited papers, and the weakest of them has citations[i]. So if citations[i] is at least n - i, there are n - i papers with at least n - i citations each, and h is at least n - i. Moving i to the right makes n - i smaller and citations[i] larger, so once the condition holds it keeps holding. The first index where it holds gives the largest possible n - i, which is the h-index. If it never holds, h is 0.
Slow approach: scan from the left
Walk the list from index 0 and return n - i at the first index that satisfies the condition. This is O(n) time and O(1) space. It is a perfectly good answer for the unsorted version once you sort, but here the input is already sorted, so a linear pass leaves performance on the table. State it, then improve it.
def h_index_scan(citations):
n = len(citations)
for i, c in enumerate(citations):
if c >= n - i:
return n - i
return 0Main approach: binary search for the first qualifying index
Search over positions 0 to n, where n means no index qualifies. Keep lo and hi so that the answer index is always inside [lo, hi]. At each step check the middle index. If it qualifies, the first qualifying index is at mid or to its left, so set hi to mid. If it does not, the first one is to the right, so set lo to mid + 1. When lo equals hi, that is the boundary, and the h-index is n - lo.
Using the half-open range with hi starting at n, rather than n - 1, handles the all-zero case without a special branch: lo walks to n and the function returns 0.
def h_index_sorted(citations):
n = len(citations)
lo, hi = 0, n
while lo < hi:
mid = (lo + hi) // 2
if citations[mid] >= n - mid:
hi = mid
else:
lo = mid + 1
return n - lofunction hIndexSorted(citations) {
const n = citations.length;
let lo = 0;
let hi = n;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (citations[mid] >= n - mid) hi = mid;
else lo = mid + 1;
}
return n - lo;
}Trace [0, 2, 3, 3, 8], where n is 5. lo = 0, hi = 5. mid = 2: citations[2] is 3 and n - 2 is 3, so it qualifies and hi becomes 2. mid = 1: 2 against 4 fails, so lo becomes 2. Now lo equals hi at 2, and the answer is 5 - 2 = 3.
The right shift in JavaScript is safe because array lengths stay far below the range where 32-bit arithmetic overflows. In a language with fixed-width integers and very large ranges, write lo + (hi - lo) / 2 instead.
Complexity
| Approach | Time | Extra space | Fits when |
|---|---|---|---|
| Try every h and count | O(n²) | O(1) | Never, beyond explaining the definition |
| Left-to-right scan | O(n) | O(1) | Sorted input, no time pressure |
| Binary search on the boundary | O(log n) | O(1) | The expected answer |
Edge cases to test
- An empty list: return 0.
- All zeros: return 0, with lo running all the way to n.
- Every paper cited more than n times: h equals n, and the boundary is index 0.
- A single paper with zero citations and a single paper with many.
- Long runs of equal values around the boundary, such as [3, 3, 3, 3, 3], where h is 3.
Common mistakes
- Returning citations[i] instead of n - i. The h-index counts papers; it is not a citation value, as [10, 20, 30] shows.
- Searching for an exact match where citations[mid] equals n - mid. Plenty of inputs have no such index: [1, 4, 4] never has a tie, yet its h-index is 2.
- Setting hi to n - 1, which forces an extra check for the case where no index qualifies.
- Writing while lo <= hi with hi = mid, which can loop forever when lo equals hi.
- Re-sorting the input. It is already sorted, and sorting adds O(n log n) for nothing.
How to explain it out loud
Fictional example
- Candidate
- For an index i, everything from i rightward is n minus i papers, and the smallest of them is citations of i. If that smallest one is at least n minus i, then h is at least n minus i.
- Candidate
- As i grows, the left side of that comparison grows and the right side shrinks, so the answers are false then true. I want the first true, which is a standard lower-bound binary search.
- Candidate
- I search on zero to n so that 'no index works' falls out as lo equal to n, which returns zero. That is logarithmic time and constant space.
Related problems in this set
The same first-true search powers Valid Perfect Square, where the predicate is whether a candidate root squared reaches the number, and Arranging Coins, where it is whether k rows fit. Pancake Sorting is the opposite kind of task, producing an order rather than searching one. The data structure interview questions page covers arrays and search in a wider setting.
Working through it with Natively
When you practice boundary searches like this one, Natively can take a screenshot of the problem and suggest the predicate and search pattern it sees, so you can compare after writing your own version. Running a mock round with it on your computer gives you a transcript, which is a quick way to confirm you defined h correctly before coding. More on an AI interview assistant for Mac and Windows.
Questions people ask
How do you solve H-Index II in O(log n)?
Binary search for the first index i where citations[i] >= n - i, using a range from 0 to n. The h-index is n minus that index, and it is 0 when no index qualifies.
Why does binary search work for H-Index II?
As the index moves right, citations[i] can only grow and n - i only shrinks, so the condition is false for a prefix and true for the rest. Binary search finds where it switches.
What is the difference between H-Index and H-Index II?
The first version gives the citations unsorted, so a sort or a counting pass is needed. The second gives them sorted ascending, which allows a logarithmic search.
Can the h-index be larger than the number of papers?
No. It counts papers, so it is at most n even when every paper has many more citations than that.
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.
