ProgrammingHardGoldman Sachs OA · TCS Digital

What is the time complexity of the following pseudocode, as a function of n (n > 2)?

What is the time complexity of the following pseudocode, as a function of n (n > 2)?
while (n > 2)
    n = squareRoot(n)      // integer square root, O(1)
end while
  1. A. O(log log n)
  2. B. O(log n)
  3. C. O(√n)
  4. D. O(1)

Correct answer: A. O(log log n)

Each step halves the exponent: n = 2^(2^k) becomes 2^(2^(k−1)). It takes about log₂ log₂ n steps to get down to 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic