Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: A researcher's h-index is the largest h such that at least h papers have ≥ h citations each.

Problem: A researcher's h-index is the largest h such that at least h papers have ≥ h citations each.
def h_index(c):
    c = sorted(c, reverse=True); h = 0
    for i, x in enumerate(c):
        if x >= i + 1: h = i + 1
        else: break
    return h
print(h_index([6, 0, 3, 5, 1, 4]), h_index([10, 10, 10]), h_index([0, 0]))
What is printed?
  1. A. 3 3 1
  2. B. 3 3 0
  3. C. 3 10 0
  4. D. 4 3 0

Correct answer: B. 3 3 0

Sorted descending: 6, 5, 4, 3, 1, 0; the i-th paper (1-based) still has ≥ i citations for i = 1, 2, 3 (4 ≥ 3) but not i = 4 (3 < 4) → 3. Three papers can give at most h = 3 even with 10 citations each. O(n log n); counting sort gives O(n).

More Coding Interview Problems questions

Exams that ask this topic