← Quiz: Segment Tree and Fenwick Tree: all 10 questions
Quiz: Segment Tree and Fenwick Tree · Question 4 of 10
What does this print? (code)
What does this print?
a = [4, 1, 3, 2]
tree = {}
def build(lo, hi):
if lo == hi:
tree[(lo, hi)] = a[lo]
else:
mid = (lo + hi) // 2
tree[(lo, hi)] = build(lo, mid) + build(mid + 1, hi)
return tree[(lo, hi)]
build(0, 3)
print(tree[(0, 3)], tree[(0, 1)], tree[(2, 3)])
- A.10 4 3
- B.4 1 3
- C.10 7 3
- D.10 5 5
Answer & explanation
Correct answer: 10 5 5
The root is 4 + 1 + 3 + 2 = 10; the left half is 4 + 1 = 5 and the right half is 3 + 2 = 5.
Can you get the rest right without peeking?
Take the timed Quiz: Segment Tree and Fenwick Tree quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →