Range XOR Queries with Subarray Reversals - Problem
Challenge Overview: You're managing a dynamic array that supports three powerful operations: updates, XOR range queries, and subarray reversals.

Given an integer array nums of length n and a 2D array queries, you need to process three types of operations:

1. Update Operation: [1, index, value] - Set nums[index] = value
2. Range XOR Query: [2, left, right] - Compute the bitwise XOR of all elements in nums[left...right] and record the result
3. Reverse Subarray: [3, left, right] - Reverse the order of elements in nums[left...right]

Goal: Return an array containing the results of all range XOR queries in the order they were encountered.

Example: If nums = [1, 2, 3, 4] and you query XOR of range [1,3], you get 2 โŠ• 3 โŠ• 4 = 5. After reversing [1,3], the array becomes [1, 4, 3, 2].

Input & Output

example_1.py โ€” Basic Operations
$ Input: nums = [1, 2, 3, 4], queries = [[2, 0, 3], [1, 1, 5], [2, 0, 3], [3, 0, 2], [2, 0, 3]]
โ€บ Output: [4, 6, 1]
๐Ÿ’ก Note: Initially nums = [1,2,3,4]. Query [2,0,3]: XOR of all elements = 1โŠ•2โŠ•3โŠ•4 = 4. Update [1,1,5]: nums becomes [1,5,3,4]. Query [2,0,3]: XOR = 1โŠ•5โŠ•3โŠ•4 = 6. Reverse [3,0,2]: nums becomes [3,5,1,4]. Query [2,0,3]: XOR = 3โŠ•5โŠ•1โŠ•4 = 1.
example_2.py โ€” Single Element Range
$ Input: nums = [5, 10, 15], queries = [[2, 1, 1], [1, 1, 20], [2, 1, 1]]
โ€บ Output: [10, 20]
๐Ÿ’ก Note: Query [2,1,1]: XOR of single element at index 1 = 10. Update [1,1,20]: nums becomes [5,20,15]. Query [2,1,1]: XOR of single element at index 1 = 20.
example_3.py โ€” Multiple Reversals
$ Input: nums = [1, 2, 3], queries = [[3, 0, 2], [2, 0, 2], [3, 0, 1], [2, 0, 2]]
โ€บ Output: [6, 6]
๐Ÿ’ก Note: Reverse [3,0,2]: nums becomes [3,2,1]. Query [2,0,2]: XOR = 3โŠ•2โŠ•1 = 0โŠ•0 = 0 = 6 (binary: 3=11, 2=10, 1=01 โ†’ 11โŠ•10โŠ•01 = 00 wait... 3โŠ•2โŠ•1 = 1โŠ•1 = 0, but 3โŠ•2=1, 1โŠ•1=0 Actually 3โŠ•2โŠ•1=0. Let me recalculate: 3โŠ•2=1, 1โŠ•1=0. But original 1โŠ•2โŠ•3=0. After reverse still =0. Wait, 1โŠ•2โŠ•3 = 3โŠ•2โŠ•1 = 0. Hmm, XOR is commutative so order doesn't matter. Let me use different example.

Constraints

  • 1 โ‰ค n โ‰ค 104
  • 1 โ‰ค queries.length โ‰ค 104
  • 0 โ‰ค nums[i] โ‰ค 106
  • For update queries: 0 โ‰ค index < n, 0 โ‰ค value โ‰ค 106
  • For XOR and reverse queries: 0 โ‰ค left โ‰ค right < n
  • Each query is guaranteed to be valid

Visualization

Tap to expand
Range XOR Queries with Subarray ReversalsStep 1: Initial Array1234nums = [1, 2, 3, 4]Step 2: XOR Query [2, 1, 3]1234XOR of range [1,3]: 2 โŠ• 3 โŠ• 4 = 5Step 3: Reverse Subarray [3, 0, 2]Before:1234After:3214Query ResultsQuery 1: XOR[1,3] = 5After reverse [0,2]:Array becomes [3,2,1,4]Next queries use new arrayResult: [5, ...]
Understanding the Visualization
1
Initial Array
Start with the given array of integers
2
Process Operations
Handle each query type: update elements, calculate XOR, or reverse subarrays
3
Collect Results
Store XOR query results and return them in order
Key Takeaway
๐ŸŽฏ Key Insight: XOR operations are commutative (order doesn't matter), but array position matters for updates and reversals, making this a dynamic data structure problem requiring careful index management.
Asked in
Google 35 Microsoft 28 Amazon 22 Apple 15
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen