DSA Tracker

Medium

Subarray with Given XOR

A medium Arrays problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Arrays
Sheets
1
Core for
25 roles
Platform
GeeksforGeeks

The problem

Given an array of integers and a target value k, count the number of contiguous subarrays whose XOR equals k.

Example 1

Input
[4,2,2,6,4], k=6
Output
4

Example 2

Input
[5,6,7,8,9], k=5
Output
1

Example 3

Input
[1,1,1], k=1
Output
3

Constraints

  • 1 <= n <= 10^5
  • 0 <= arr[i] <= 10^9
  • 0 <= k <= 10^9

How to think about it

Updated 2026-09-09

The XOR of any range arr[i..j] is prefixXor[j] ^ prefixXor[i - 1]. If that range equals k, then prefixXor[i - 1] must equal prefixXor[j] ^ k. Tracking the frequency of previously observed prefix XORs in a hash table turns an interval search into an instant frequency lookup at every step.

Approaches, worst first

  1. Nested interval evaluation

    time O(n^2) · space O(1)

    Iterate over all possible start indices i and end indices j, computing the subarray XOR incrementally. Increments a running count whenever the range XOR equals k, but requires inspecting all quadratic combinations.

  2. Prefix XOR hash mapWrite this one

    time O(n) · space O(n)

    Maintain running prefix XOR and a hash map storing the frequencies of seen prefixes, initialized with {0: 1}. For each element, look up prefixXor ^ k in the map to add matching prefix counts, then increment prefixXor in the map.

Where people lose marks · 3
  • Omitting the initial base case `{0: 1}` in the map skips valid subarrays that start at index 0 whose total XOR equals k directly.
  • Updating the map with the current prefix XOR before querying for `prefixXor ^ k` incorrectly counts empty subarrays when k equals 0.
  • Total count of matching subarrays can reach n * (n + 1) / 2 = 5 * 10^9 when all values and k are 0, overflowing 32-bit signed integers.

The theory behind it

Arrays — the ground this problem stands on. All Arrays problems

What Arrays is

An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.

When to reach for it

Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.

How the pattern works

Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.

What each operation costs

OperationTime
look up element by indexO(1)
insert or delete at the startO(n)
search an unsorted collection for a valueO(n)
What usually goes wrong with Arrays
  • Reading past the final index by checking index less than or equal to length instead of strictly less than length, triggering index out of bounds exceptions.
  • Modifying length or removing elements during a forward iteration loop, which causes remaining items to shift left and skip validation on the next neighbor.
  • Assuming dynamic resizing is costless inside nested loops, causing repeated memory reallocation copies when appending unknown quantities of items.

Which roles need this problem

Arrays is a core topic for these 25 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 3 more roles, including Database Engineer, Bioinformatics Engineer, Networking Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Arrays problems

Problem set and role mapping as of .