Bird
Raised Fist0
Interview PreptrieshardGoogleAmazon

Count Pairs With XOR in a Range

Choose your preparation mode4 modes available
</>
IDE
def countPairs(nums: list[int], low: int, high: int) -> int:public int countPairs(int[] nums, int low, int high)int countPairs(vector<int>& nums, int low, int high)function countPairs(nums, low, high)
def countPairs(nums, low, high):
    # Write your solution here
    pass
class Solution {
    public int countPairs(int[] nums, int low, int high) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int countPairs(vector<int>& nums, int low, int high) {
    // Write your solution here
    return 0;
}
function countPairs(nums, low, high) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: 0Fails to count any pairs, possibly returns 0 for all inputs.✅ Ensure nested loops or trie counting logic is implemented to count pairs properly.
Wrong: Incorrect count missing pairs with XOR exactly equal to low or highUsing strict inequalities instead of inclusive range checks.✅ Change condition to 'if low <= xor_val <= high' instead of strict inequalities.
Wrong: TLEUsing brute force O(n^2) approach on large inputs.✅ Implement bit trie based counting with O(n*log M) complexity.
Wrong: Wrong count on empty or single-element arraysNot handling base cases where no pairs exist.✅ Return 0 immediately if nums length < 2.
Wrong: Incorrect count with duplicates or all zerosNot counting all pairs when XOR is zero and range includes zero.✅ Count all pairs using n*(n-1)/2 when XOR=0 is in range.
✓
Test Cases
t1_01basic
Input{"nums":[1,4,2,7],"low":2,"high":6}
Expected6

Pairs with XOR in [2,6] are (1,4)=5, (1,2)=3, (4,2)=6, (4,7)=3, (2,7)=5, (1,7)=6

t1_02basic
Input{"nums":[3,8,5,6],"low":1,"high":4}
Expected1

Pairs with XOR in [1,4]: (5,6)=3 only.

t2_01edge
Input{"nums":[],"low":0,"high":0}
Expected0

Empty array has no pairs, so count is 0.

t2_02edge
Input{"nums":[5],"low":0,"high":10}
Expected0

Single element array has no pairs, so count is 0.

t2_03edge
Input{"nums":[0,0,0,0],"low":0,"high":0}
Expected6

All pairs XOR to 0, which is within [0,0]. Number of pairs = C(4,2) = 6.

t2_04edge
Input{"nums":[1000000,999999,500000],"low":0,"high":1000000}
Expected3

All pairs XOR values are within [0,1000000], so count all pairs: C(3,2)=3.

t3_01corner
Input{"nums":[1,2,3,4,5],"low":0,"high":1}
Expected2

Pairs with XOR in [0,1]: (2,3)=1 and (4,5)=1.

t3_02corner
Input{"nums":[1,1,1,1],"low":0,"high":0}
Expected6

All pairs XOR to 0, which is within [0,0]. Number of pairs = C(4,2) = 6.

t3_03corner
Input{"nums":[0,1023,1024,2047],"low":512,"high":1536}
Expected4

Pairs with XOR in [512,1536]: (0,1023)=1023, (0,1024)=1024, (1023,2047)=1024, (1024,2047)=1023.

t4_01performance
Input{"nums":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],"low":0,"high":1000000}
⏱ Performance - must finish in 2000ms

n=100, O(n*log M) expected to run within 2s; brute force O(n^2) will TLE.

Practice

(1/5)
1. Given the following Trie implementation, after inserting the words ['cat', 'car', 'dog'], what is the value of node.isEnd after processing the last character of 'car' in the insert method?
easy
A. false, because 'car' shares prefix with 'cat' and 'dog'
B. true, but only if 'car' was inserted before 'cat'
C. false, because isEnd is only set after inserting all words
D. true, because 'car' is a complete inserted word

Solution

  1. Step 1: Trace insertion of 'car'

    For each character 'c', 'a', 'r', nodes are created or reused. After last character 'r', node.isEnd is set to true.
  2. Step 2: Confirm isEnd flag meaning

    isEnd marks the end of a complete word. Since 'car' was inserted, node.isEnd after 'r' is true.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    isEnd is true after inserting 'car' [OK]
Hint: isEnd is set true after inserting each complete word [OK]
Common Mistakes:
  • Assuming isEnd is false if prefix overlaps with other words
2. Consider the following Python code implementing a bit trie for maximum XOR queries. Given nums = [1, 2] inserted into the trie, what is the value of max_xor after calling max_xor(3)?
easy
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Trace insertion of nums 1 and 2

    Bits of 1 and 2 inserted from MSB to LSB into trie.
  2. Step 2: Trace max_xor(3)

    For each bit of 3, try toggled bit in trie to maximize XOR. At highest differing bit, toggled bit exists, setting max_xor bits accordingly.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    3 XOR 0 = 3, 3 XOR 1 = 2, 3 XOR 2 = 1; max is 3 [OK]
Hint: XOR with 3 and 0,1,2 yields max 3 [OK]
Common Mistakes:
  • Confusing bit order in trie traversal
  • Off-by-one in bit indexing
  • Returning wrong max_xor value
3. Consider the following code snippet implementing the optimal trie-based solution for shortest unique prefixes. Given the input words = ["dog", "dove", "duck"], what is the output of shortest_unique_prefix(words)?
easy
A. ["do", "dov", "du"]
B. ["d", "dov", "du"]
C. ["dog", "dove", "duck"]
D. ["dog", "dov", "du"]

Solution

  1. Step 1: Trace insertion frequencies

    Insert "dog": d(1), o(1), g(1); "dove": d(2), o(2), v(1), e(1); "duck": d(3), u(1), c(1), k(1).
  2. Step 2: Find unique prefixes

    For "dog": prefix "dog" (g freq=1) unique; for "dove": prefix "dov" (v freq=1) unique; for "duck": prefix "du" (u freq=1) unique.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Prefixes match first node with freq=1 [OK]
Hint: Unique prefix ends where node freq equals 1 [OK]
Common Mistakes:
  • Stopping prefix too early
  • Assuming first letter always unique
4. You are given a 2D grid of letters and a list of words. You need to find all words from the list that can be formed by sequentially adjacent letters on the board, where adjacent letters are horizontally or vertically neighboring. Each letter cell may only be used once per word. Which algorithmic approach is best suited to efficiently solve this problem when the word list is large?
easy
A. Build a Trie from the word list and perform a single DFS on the board with prefix pruning using the Trie.
B. Use a brute force DFS for each word independently, checking all paths on the board.
C. Use dynamic programming to store subproblems of partial word matches on the board.
D. Sort the words and use binary search combined with DFS to find matches on the board.

Solution

  1. Step 1: Understand problem constraints and prefix sharing

    Multiple words need to be found on the board, and many words may share prefixes, making prefix pruning beneficial.
  2. Step 2: Use Trie for efficient prefix pruning during DFS

    Building a Trie allows pruning DFS paths early if no word starts with the current prefix, avoiding redundant searches and improving efficiency.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Trie + DFS reduces search space drastically compared to checking each word separately [OK]
Hint: Trie + DFS prunes invalid prefixes early [OK]
Common Mistakes:
  • Thinking brute force DFS per word is efficient
  • Assuming DP applies here
  • Using sorting and binary search without prefix pruning
5. Examine the following buggy code snippet for building the trie and collecting suggestions. Which line contains the subtle bug that can cause incorrect suggestion ordering or runtime errors?
medium
A. Line with 'products.sort()' missing before building trie
B. Line with 'for product in products:' loop
C. Line with 'if len(node.suggestions) < 3:' condition
D. Line with 'if node and ch in node.children:' during search

Solution

  1. Step 1: Identify missing sorting

    The code does not sort products before building the trie, so suggestions are appended in input order, not lex order.
  2. Step 2: Consequences of missing sort

    Without sorting, the suggestions list at each node is not guaranteed to be lexicographically smallest three, violating problem requirements.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting before trie build is essential for correct lex order [OK]
Hint: Always sort products before building trie for lex order [OK]
Common Mistakes:
  • Forgetting to sort products
  • Appending suggestions without limit
  • Not handling missing prefix nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard