Magicsheet logo

Substring XOR Queries

Medium
100%
Updated 6/1/2025

Substring XOR Queries

What is this problem about?

The "Substring XOR Queries" problem involves a binary string and a set of queries. Each query provides two integers, first and second. Your task is to find a substring within the binary string such that the XOR sum of its decimal value and first equals second. Essentially, you are looking for a substring whose binary representation, when converted to a decimal integer, matches the result of first ^ second. If multiple such substrings exist, usually the one with the smallest starting index or shortest length is preferred.

Why is this asked in interviews?

This problem is frequently used by companies like Trilogy to test a candidate's proficiency in bitwise operations and efficient string searching. It requires translating a logical requirement (val ^ first == second) into a search target (val = first ^ second). The "trick" lies in realizing that since the query values are often limited (e.g., up to 10^9), the length of the binary substring we need to find is relatively small (around 30 bits). This allows for pre-calculating the positions of binary values instead of searching the string repeatedly for each query.

Algorithmic pattern used

The core pattern is Pre-computation using a Hash Table. Since we only care about substrings representing values up to a certain bit length (e.g., 30 bits for values up to 10^9), we can slide a window of varying sizes (from 1 to 30) over the binary string once. For each window, we convert the binary substring to its decimal value and store its start and end indices in a hash map. When a query arrives, we compute the target value using XOR and look it up in our map in O(1) time.

Example explanation

Suppose the binary string is "101101" and a query asks for first = 3 and second = 7. First, calculate the target value: 3 ^ 7 = 4. The binary representation of 4 is "100". Now, search the string "101101" for the first occurrence of "100". In this case, it doesn't exist. If the query were first = 1 and second = 2, the target would be 1 ^ 2 = 3. Binary 3 is "11". We find "11" starting at index 2 (0-indexed) in "101101".

Common mistakes candidates make

  1. Over-complicating the XOR logic: Candidates sometimes try to use complex data structures like Segment Trees for XOR, not realizing that val ^ first == second is simply val == first ^ second.
  2. Ignoring leading zeros: In binary strings, "011" and "11" both represent the decimal value 3. Failing to handle how the problem treats leading zeros or multiple occurrences can lead to incorrect indices.
  3. Inefficient searching: Searching the entire string for every query results in an O(Q * N) solution, which is too slow. Pre-computing the values is vital.

Interview preparation tip

Always look at the constraints of the input values. If a problem involves decimal values derived from binary strings and those values are within the range of a standard integer, it's a hint that you only need to consider substrings of a fixed, small length. This "constraint-based optimization" is a common theme in Medium and Hard coding problems.

Similar Questions