Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the largest value of x such that x^3 <= 10^12 (integer cube root)? Run the binary search.

Problem: What is the largest value of x such that x^3 <= 10^12 (integer cube root)? Run the binary search.
lo, hi = 0, 10 ** 6
while lo < hi:
    mid = (lo + hi + 1) // 2
    if mid ** 3 <= 10 ** 12:
        lo = mid
    else:
        hi = mid - 1
print(lo)
What is printed?
  1. A. 9999
  2. B. 1000000
  3. C. 10001
  4. D. 10000

Correct answer: D. 10000

10000³ = 10¹² exactly, and the search keeps the largest mid with mid³ ≤ N, so it prints 10000. int((10**12) ** (1/3)) gives 9999 because of floating-point error, 10⁶ is the square root, and 10001 is the first value that fails.

More Coding Interview Problems questions

Exams that ask this topic