Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Binary search

Research impact from sorted citation counts

Medium · Target 15 minutes · Binary search · Type asked atGoogleTwo Sigma

A researcher's citations are given in non-decreasing order: citations[i] is the number of citations of their i-th paper. Their h-index is the largest h such that at least h papers have at least h citations each. Return it, using O(log n) steps.

Examples

InputOutputWhy
citations = [0, 1, 3, 5, 6]3Three papers (3, 5, 6) have at least 3 citations; there are not four with at least 4.
citations = [1, 2, 100]2
citations = [0]0

Constraints

  • 1 ≤ n ≤ 10⁵
  • 0 ≤ citations[i] ≤ 1000
  • citations is non-decreasing

Hints

Hint 1

Because the list is sorted, the papers from index i to the end are the n − i most cited. When do they all have at least n − i citations?

Hint 2

They do exactly when citations[i] ≥ n − i, since citations[i] is the smallest of them. Is that test monotone in i?

Hint 3

As i grows, citations[i] cannot fall and n − i falls, so once the test passes it keeps passing. Find the first i where it passes; the answer is n − i.

Where people lose marks

  • Returning citations[i*] instead of n − i*. The h-index is a count of papers, not a citation number.
  • Using a closed range and missing the case where no index passes (all zeros): the answer is then 0.
  • Testing citations[i] > n − i: the definition says at least, so the comparison is ≥.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.