Skip to content
MediumTrieAI interview only

Maximum Subarray XOR

Asked atgoogleamazonmicrosoftgoldman-sachs

01 · Problem

Given an integer array nums, return the maximum value of nums[i] ^ nums[i+1] ^ ... ^ nums[j] over all contiguous non-empty subarrays (0 <= i <= j < nums.length), where ^ is bitwise XOR.

02 · Examples

Example 01
Input
nums = [1,2,3,4]
Output
7

The subarray [3,4] gives 3 ^ 4 = 7, which is the largest possible.

Example 02
Input
nums = [8,1,2,12,7,6]
Output
15

The subarray [1,2,12] gives 1 ^ 2 ^ 12 = 15.

Example 03
Input
nums = [4,6]
Output
6

The single-element subarray [6] gives 6, larger than 4 and 4 ^ 6 = 2.

03 · Constraints

  • 011 <= nums.length <= 105
  • 020 <= nums[i] < 220
  • 03The chosen subarray must be contiguous and non-empty

04 · Optimal complexity

Time
O(n * B)
Space
O(n * B)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.