Uber | Onsite | XOR of leaf nodes

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-02-29Added By Anonymous
Source: Link

Company: Uber

Role: software engineer i, software engineer ii

Interview Stage: Onsite


Original Post Content:


Given a tree with root node id starting with 1. Each node has a value associated with it.


We are given function f where f(n) = xor of values of nodes on path from root to node n.


Given a variable G = xor of f(l) where l is as leaf node.


We are given a list of queries of form {n,p} where p is the new parent of node n. Basically, we change parent of node n from its current parent to node p.


Each query is independent and non-persistant. So, tree reverts back to original after each query.


Return values of G after each query


Example:


//vector of parent -child { parent , child}


{1,2}, {1,3},{2,4},{2,5},{3,6},{3,7},{4,8}


//values for each node


{1,2,3,4,5,6,7,8}


Initial G= 4^5^6^7^8


Query 1: - {4,3}. //change parent of 4 from 2 to 3


Answer => G = 2^3^4^5^6^7^8

Comments