Append and XOR Array Queries

Quick Overview

Process a list of queries that either append a value to an array or XOR every current element with a value, then return the final array in insertion order. Tests whether you can replace a costly per-element update with a running XOR accumulator and reason about the self-inverse property of XOR.

Append and XOR Array Queries

Company: Rubrik

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You start with an empty array `arr`. You are given a 2D integer array `queries` where each `queries[i] = [type, value]`. Process the queries in order: - If `type == 1`, append `value` to the end of `arr`. - If `type == 2`, replace every element currently in `arr` with its bitwise XOR with `value`. An XOR operation only affects elements that are present in the array at the moment it is applied. Elements appended later must not be affected by earlier XOR operations. Return the final array `arr` in insertion order. ### Function Signature ```python def process_queries(queries: list[list[int]]) -> list[int]: ... ``` ### Rules - Each query is a pair `[type, value]`, processed from first to last. - A type-2 query on an empty array changes nothing. - If there is no type-1 query, return an empty list. ### Constraints - `1 <= queries.length <= 2 * 10^5` - `queries[i].length == 2` - `queries[i][0]` is either `1` or `2` - `0 <= queries[i][1] < 2^30` ### Examples ```text Input: queries = [[1,5],[2,3],[1,7],[2,1]] Output: [7,6] ``` The array becomes `[5]`, then `[6]` after the XOR with 3, then `[6, 7]`. The final XOR with 1 turns 6 into 7 and 7 into 6. ```text Input: queries = [[2,4],[1,1],[1,2],[2,7]] Output: [6,5] ``` The first XOR runs on an empty array and has no effect. After the two appends the array is `[1, 2]`, and XOR with 7 gives `[6, 5]`.

Overview: Process a list of queries that either append a value to an array or XOR every current element with a value, then return the final array in insertion order. Tests whether you can replace a costly per-element update with a running XOR accumulator and reason about the self-inverse property of XOR.

Rubrik logo
Rubrik
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You start with an empty array arr. You are given a 2D integer array queries where each queries[i] = [type, value]. Process the queries in order:

  • If type == 1 , append value to the end of arr .
  • If type == 2 , replace every element currently in arr with its bitwise XOR with value .

An XOR operation only affects elements that are present in the array at the moment it is applied. Elements appended later must not be affected by earlier XOR operations.

Return the final array arr in insertion order.

Function Signature

def process_queries(queries: list[list[int]]) -> list[int]:
    ...

Rules

  • Each query is a pair [type, value] , processed from first to last.
  • A type-2 query on an empty array changes nothing.
  • If there is no type-1 query, return an empty list.

Constraints

  • 1 <= queries.length <= 2 * 10^5
  • queries[i].length == 2
  • queries[i][0] is either 1 or 2
  • 0 <= queries[i][1] < 2^30

Examples

Input: queries = [[1,5],[2,3],[1,7],[2,1]]
Output: [7,6]

The array becomes [5], then [6] after the XOR with 3, then [6, 7]. The final XOR with 1 turns 6 into 7 and 7 into 6.

Input: queries = [[2,4],[1,1],[1,2],[2,7]]
Output: [6,5]

The first XOR runs on an empty array and has no effect. After the two appends the array is [1, 2], and XOR with 7 gives [6, 5].