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- A. O(log log n)
- B. O(log n)
- C. O(√n)
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer coins[3] = {1, 2, 5} Integer ways[0..6] = all 0 ways[0] = 1 for (each a from…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code; strings are indexed from 0) String s =…
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL Node slow = head, fast = head while…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer j for (each d from 0 to…
- What will be the output of the following pseudocode? (strings are indexed from 0) Procedure perm(String s, Integer l) // s is passed by…
- What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first) Integer…