Data Structures & AlgorithmsHardProduct company OA
In a Fenwick (binary indexed) tree of size 16 (1-based), the update at index 5 adds the value to i = 5 and then repeatedly does i += i & (−i) while i ≤ 16. Which indices are updated?
- A. 5, 6, 8, 16
- B. 5, 6, 7, 8
- C. 5, 4, 0
- D. 5, 7, 8, 16
Correct answer: A. 5, 6, 8, 16
i & (−i) is the lowest set bit: 5 (0101) + 1 = 6; 6 (0110) + 2 = 8; 8 + 8 = 16; 16 + 16 = 32 > 16, stop. Each of those nodes covers a range that contains index 5. '5, 4, 0' is the QUERY direction (i −= i & −i).
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
- Union–find with NO union by rank/size and NO path compression: union(a, b) sets parent[find(a)] = find(b). Starting with singletons 0–5…
- A connected undirected graph has vertex degrees A: 3, B: 4, C: 2, D: 3, E: 2. Does it have an Euler path (using every edge exactly once)…
- Houses in a row hold [3, 2, 7, 10, 1, 9]. No two ADJACENT houses may both be chosen. What is the maximum total? (dp[i] = max(dp[i − 1]…
- The set [3, 9, 7, 3, 11] is split into two subsets so that the absolute difference of their sums is as small as possible (subset-sum DP…
- Digits map to letters A = 1 … Z = 26. In how many ways can the string '2263' be decoded? (dp over prefixes; a pair counts only if it is…
- Keys 50, 30, 20, 10, 40, 70, 60 are inserted into an empty BST. An iterative inorder traversal pushes nodes while walking left and pops to…