Data Structures & AlgorithmsModerateCodeforces Div2 B · prefix techniques
Problem: What is the complexity of computing all prefix XOR values of an array of n = 10^6 integers and then answering q = 10^6 range-XOR queries online?
- A. O(n * q) by recomputing each range
- B. O(n + q) using prefix XOR, since x XOR x = 0 lets each query use two prefix values
- C. O(n log q) with a segment tree needed
- D. O(q * sqrt(n)) for every query
Correct answer: B. O(n + q) using prefix XOR, since x XOR x = 0 lets each query use two prefix values
Range XOR of [l, r] equals pre[r+1] XOR pre[l], because equal values cancel. The trap is using a segment tree when the operation is invertible.
More Coding Interview Problems questions
- Problem: Consider a DP over subsets for assigning n jobs to n workers (cost matrix given), with state = mask of assigned jobs and…
- Problem: A string of length n is built using the operation 'append the reverse of the current string'. Starting with 'a', the length after…
- Problem: Which statement about Dijkstra's algorithm using a binary heap with lazy deletion (pushing duplicate entries and skipping stale…
- Problem: What does the following print? python x = 5 print(x << 3 | x >> 1, x & 6, x ^ 3)
- Problem: For the string 'mississippi', what is the number of distinct substrings of length 2 that occur at least twice? Run the code…
- Problem: How many Armstrong numbers (numbers equal to the sum of their digits each raised to the power of the digit count) lie in [1…