XOR Query

Assume you initially have an empty array say ‘ARR’.

You need to return the updated array provided that some ‘Q’ number of queries were performed on this array.

The queries are of two types:

1. 1 ‘VAL’, for this type of query, you need to insert the integer 'VAL' to the end of the array.
2. 2 ‘VAL’, for this type of query, you need to take the bitwise XOR of all the elements of the array with 'VAL' i.e each element of the array ‘ARR’ will be updated as ‘ARR[i]’ = ‘ARR[i]’ ^ ‘VAL’ ( ^ denotes the bitwise XOR operation).

Note:

1) Bitwise XOR operation takes two numbers and performs XOR operation on every bit of those two numbers. For example, consider two numbers 2 and 3 their bitwise XOR will be 1. Because the binary representation of 2 is '10' and the binary representation of 3 is '11'. And XOR of '10' and '11' will be '01'(because XOR evaluates to 0 if the corresponding bits are the same in both the operands, otherwise it evaluates to 1), which is equal to 1.

2) The first query will always be a type 1 query.

3) Note that the ith query should be performed on the array obtained after performing (i-1)th query on the array and so on i.e the changes of each query are updated on the original array itself.
Input Format:
The first line contains an integer ‘T’ which represents the number of test cases.

The first line of each test case contains an integer ‘Q’ denoting the number of queries. 

Then each of the ‘Q’ lines contains two space-separated integers denoting the query to be performed.
Output Format:
For each test case, return the updated array after processing all the queries.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function. 
Constraints:
1 <= T <= 10
1 <= Q <= 10^5
1 <= Val <= 10^9

Time Limit: 1sec
Follow Up:
Can you solve this in constant i.e O(1) space complexity? Space used to return the list will not be counted as an extra space.
AnswerBot
1y

The problem requires updating an array based on a series of queries, where each query can either insert a value or perform a bitwise XOR operation on all elements.

  • Use a loop to iterate through each qu...read more

CodingNinjas
author
2y
Brute Force

Approach:

  • It is a brute force approach where we will create an empty array initially of size 10^5 + 1.
  • Now, whenever we have a query of type 1 we will insert the value of VAL in the array. An...read more
CodingNinjas
author
2y
Prefix Array

Approach:

  • We derive the basic idea for this approach from the problem where we are given a range L to R and we have to add a number to all the elements in this range in the given array, to ...read more
CodingNinjas
author
2y
Properties of XOR

Approach:

  • In this approach, we basically try to optimize the space using the property of XOR that is ‘A(XOR)A’ = 0 where A is an integer.
  • So here we maintain a variable named flag, init...read more
Add answer anonymously...
Texas Instruments Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter