🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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)])
  1. A.10 4 3
  2. B.4 1 3
  3. C.10 7 3
  4. 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 →