Data Structures & AlgorithmsModerateGATE CS · competitive programming
For (i = 1; i ≤ n; i *= 3) with n = 1000, how many times does the body run?
- A. 7
- B. 6
- C. 8
- D. 333
Correct answer: A. 7
i takes 1, 3, 9, 27, 81, 243, 729 (the next value 2187 exceeds 1000), giving 7 iterations, which is ⌊log₃ 1000⌋ + 1.
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
- 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…
- What is the area of the largest rectangle in the histogram with bar heights [2, 4, 2, 1, 6, 5, 5]?
- In the circular array [1, 5, 3, 4, 2], what is the next greater element of the value 2 at index 4, when the search wraps past the end?