Open In App

Google Online Challenge 2020

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Google online challenge 2020 for summer internships 2021 was held on August 16. It was a 60-minute online test having 2 questions to code.

First Question: Size of the smallest subset with maximum Bitwise OR

Second Question: Given a list which initially contains 0, the following queries can be performed:

  • 0 X: add X to the list
  • 1 X: replace each element “A” of the list by A^X, where ^ is the xor operator.

Return a list with the result elements in increasing order.

Example:

5 (no of queries)
0 4
0 2
1 4
0 5
1 8

Answer:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int q;
        cin >> q;
        priority_queue<pair<int, int> > pq;
        vector<int> v;
        pq.push({
            0,
            0,
        });
        v.push_back(0);
        while (q--) {
            int a, x;
            cin >> a >> x;
            if (a == 0)
                v.push_back(x);
            else {
                pq.push({ v.size(), x });
            }
        }
        int x = 0;
        auto y = make_pair(0, 0);
        while (pq.top() != y) {
            auto top = pq.top();
            x ^= top.second;
            pq.pop();
            while (pq.top().first == top.first) {
                x ^= pq.top().second;
                pq.pop();
            }
            for (int i = top.first - 1; i >= pq.top().first;
                 i--) {
                v[i] ^= x;
            }
        }
        sort(v.begin(), v.end());
        for (auto a : v)
            cout << a << " ";
    }
}


Python3




import heapq
 
def main():
    t = int(input("Enter the number of test cases: "))
     
    # Loop through each test case
    for _ in range(t):
        q = int(input())
         
        # Initialize a priority queue and a list
        pq = [(0, 0)]
        v = [0]
         
        # Process each query
        for _ in range(q):
            a, x = map(int, input().split())
             
            if a == 0:
                v.append(x)
            else:
                heapq.heappush(pq, (len(v), x))
         
        x = 0
        y = (0, 0)
         
        # Process the priority queue
        while pq[0] != y:
            top = heapq.heappop(pq)
            x ^= top[1]
             
            # Pop elements with the same priority
            while pq[0][0] == top[0]:
                x ^= heapq.heappop(pq)[1]
             
            # Update the list
            for i in range(top[0] - 1, pq[0][0] - 1, -1):
                v[i] ^= x
         
        # Sort and print the final list
        v.sort()
        for a in v:
            print(a, end=" ")
        print()
 
if __name__ == "__main__":
    main()


8 12 13 14



Last Updated : 28 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads