Data Structures & AlgorithmsHardGATE CS · competitive programming
For (i = 1; i < n; i *= 2) and, inside it, (j = 0; j < i; j++), with n = 1024, how many times does the inner body run in total?
- A. 1024
- B. 2047
- C. 1023
- D. 512
Correct answer: C. 1023
i takes the values 1, 2, 4, …, 512, so the inner loop runs 1 + 2 + … + 512 = 1023 times. The total is O(n), not O(n log n).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many times does the body of for (i = 2; i i ≤ n; i++) run for n = 10⁶?
- For (i = 1; i ≤ n; i = 3) with n = 1000, how many times does the body run?
- Naive recursion fib(n) = fib(n−1) + fib(n−2), with base cases n < 2, makes calls to itself. How many calls in total does fib(10) make…
- rec(n) returns at once if n ≤ 1; otherwise it calls rec(n/2) twice with integer division. How many calls are made for n = 16, counting the…
- A singly linked list has a tail of 4 nodes leading into a cycle of 6 nodes. Floyd's method moves a slow pointer 1 step and a fast pointer…
- Reverse each complete group of k = 3 nodes in the list 1 → 2 → … → 10, leaving a final incomplete group as it is. What value is at…