0
0
Data Structures Theoryknowledge~10 mins

Fenwick trees (Binary Indexed Trees) in Data Structures Theory - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize a Fenwick tree array of size n+1.

Data Structures Theory
fenwicks = [0] * [1]
Drag options to blanks, or click blank then click option'
An+1
Bn-1
Cn
D2*n
Attempts:
3 left
💡 Hint
Common Mistakes
Using size n causes index errors because index 0 is unused.
Using size n-1 is too small and misses elements.
2fill in blank
medium

Complete the code to get the least significant bit (LSB) of an integer x.

Data Structures Theory
lsb = x & [1]
Drag options to blanks, or click blank then click option'
A-x
B1
Cx
D~x
Attempts:
3 left
💡 Hint
Common Mistakes
Using x & x returns x itself, not the LSB.
Using x & 1 only checks the last bit, not the full LSB.
3fill in blank
hard

Fix the error in the Fenwick tree update function to correctly move to the next index.

Data Structures Theory
def update(i, delta):
    while i < len(fenwicks):
        fenwicks[i] += delta
        i += [1]
Drag options to blanks, or click blank then click option'
Ai | (-i)
Bi + 1
Ci & i
Di & (-i)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 instead of the LSB causes incorrect updates.
Using bitwise OR or AND with wrong operands breaks the logic.
4fill in blank
hard

Fill both blanks to compute the prefix sum up to index i in a Fenwick tree.

Data Structures Theory
def prefix_sum(i):
    result = 0
    while i > 0:
        result += fenwicks[[1]]
        i -= [2]
    return result
Drag options to blanks, or click blank then click option'
Ai
Bi & (-i)
Ci + 1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i + 1 as index causes out-of-range errors.
Adding or subtracting wrong values breaks the prefix sum logic.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each index to its Fenwick tree prefix sum if the sum is positive.

Data Structures Theory
result = [1]: prefix_sum([2]) for [3] in range(1, n+1) if prefix_sum([2]) > 0}
Drag options to blanks, or click blank then click option'
Ai
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes NameError.
Using index instead of i breaks the comprehension.