Data Structures & AlgorithmsHardProduct-company MCQ pattern
What is the time complexity of g(n)?
What is the time complexity of g(n)?
def g(n):
if n <= 1:
return
for i in range(n):
pass # O(1)
g(n // 2)
g(n // 2)
g(n // 2)- A. Θ(n^log₂3) ≈ Θ(n^1.585)
- B. Θ(n²)
- C. Θ(n log n)
- D. Θ(n)
Correct answer: A. Θ(n^log₂3) ≈ Θ(n^1.585)
T(n) = 3T(n/2) + n. Since n^(log₂ 3) ≈ n^1.585 grows faster than f(n) = n, case 1 of the master theorem gives Θ(n^log₂3). With two calls it would be Θ(n log n), the tempting answer.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- How many of these statements about DFS are correct? (i) In DFS of an undirected graph, every non-tree edge is a back edge (there are no…
- Keys are inserted into an empty BST in the order 45, 15, 79, 90, 10, 55, 12, 20, 50. What is the postorder traversal? python def insert(t…
- For the same kind of BST, what is the height (edges on the longest root-to-leaf path) after inserting 1, 2, 3, 4, 5, 6, 7 in order, versus…
- A max-heap is built with heapify on the array below (0-based, children 2i+1 and 2i+2). What is the resulting array? python def sift(a, i…
- From the max-heap [50, 30, 40, 10, 20, 35, 25], delete the maximum once. What is the array? python import heapq h = [-x for x in [50, 30…
- What is printed? (count leaves, internal nodes and full nodes of a binary tree) python T = {1: (2, 3), 2: (4, None), 3: (5, 6), 4: (None…