XOR Query on Tree Problem

Given a tree with a root at node 0 and N vertices connected with N-1 edges, and an array QUERY of size Q, where each element in the array represents a node in the tree. For each node in the QUERY array, determine the XOR of all values of nodes within its subtree.

Example:

Input:
T = 1 
N = 3, Q = 2
Edges:
0 1
1 2
Query: 0 1
Output:
2 XOR value for subtrees of nodes 0 and 1 respectively
Explanation:

If the array of node values is assumed to be [value0, value1, value2], the subtree rooted at 0 would include all nodes, hence the XOR would be value0 ^ value1 ^ value2. For the subtree rooted at 1, it would include nodes 1 and 2, hence the XOR would be value1 ^ value2.

Constraints:

  • 1 <= T <= 10
  • 0 <= N, Q <= 105
  • 0 <= QUERY[i] < N

The XOR operation is performed using the bitwise operator (^).

Note:

No need to print anything as it is handled elsewhere, just implement the respective function to obtain the desired results.

AnswerBot
1y

This question is about finding the XOR of all values of nodes in the sub-tree of a given node in a tree.

  • Read the input values for the number of test cases, number of nodes, and number of queries.

  • Const...read more

Help your peers!
Select
Add answer anonymously...

Protium Finance Software Developer interview questions & answers

A Software Developer was asked Q. Search In Rotated Sorted Array Problem Statement Given a sorted array of distinc...read more
A Software Developer was asked Q. What is the garbage collector in Java?
A Software Developer was asked Q. What is the difference between an abstract class and an interface in Object-Orie...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Search In Rotated Sorted Array Problem Statement Given a sorted array of distinc...read more
A Software Developer was asked Q2. What is the garbage collector in Java?
A Software Developer was asked Q3. What is the difference between an abstract class and an interface in Object-Orie...read more
Protium Finance Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits