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?- A. 9999
- B. 1000000
- C. 10001
- 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
- Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code. python print(sum(1 for x in range(1, 1001) if…
- Problem: Which algorithm finds the convex hull of n points in O(n log n) by sorting and building lower and upper chains?
- Problem: What is the expected linear-time algorithm for the smallest enclosing circle of n points?
- Problem: Which data structure answers range-GCD queries with point updates in O(log n) each?
- Problem: What is the complexity of computing the n-th Fibonacci number modulo a prime p using matrix exponentiation?
- Problem: Which technique counts pairs in an array whose XOR is less than K in O(n bits) time?