Data Structures & AlgorithmsVery hardAmazon · Microsoft OA
What is the time complexity of this loop?
What is the time complexity of this loop?
for i in range(1, n + 1):
j = 1
while j * j <= i:
j += 1- A. Θ(n log n)
- B. Θ(n²)
- C. Θ(n^1.5)
- D. Θ(n)
Correct answer: C. Θ(n^1.5)
For each i the inner loop runs about √i times, and Σ√i for i ≤ n ≈ (2/3)n^1.5. Recognising the √i bound (rather than log i or i) is the key.
More Coding Interview Problems questions
- What is the solution of T(n) = 2T(√n) + log n?
- A hash table with separate chaining and simple uniform hashing has n keys in m buckets (load factor α = n/m). What is the expected time of…
- An LRU cache must support get and put in O(1). Which design achieves this?
- Edge case: identity versus equality in Python. python a = [1, 2]; b = [1, 2]; c = a print(a == b, a is b, a is c, [] is []) What is printed?
- Edge case: chained comparisons and booleans as integers in Python. python print(1 < 2 > 1.5, (1 < 2) > 1.5, True + True, 3 > 2 == 2) What…
- Edge case: closures over a loop variable declared with var versus let in JavaScript. javascript const a = [], b = []; for (var i = 0; i <…