Medium: Subarray with a given XOR

Asked atBoston Consulting Group
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Maintain XOR prefix through current index
  2. 02Derive needed earlier prefix as prefix xor target
  3. 03Count matching prefixes in a map
  4. 04Seed prefix zero with frequency one
  5. 05Insert current prefix after counting

Reference answer

Then expect these follow-ups

  • How would you count subarrays with sum k instead?

    Tests: follow-up reasoning

  • Why can the result exceed 32-bit range?

    Tests: correctness reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question