Decode XORed Array
Problem Statement
A hidden array arr has n non-negative numbers. It was turned into a shorter array encoded, where each entry is the XOR of two neighbours in arr: encoded[i] = arr[i] ^ arr[i + 1].
You get encoded and the first value of the hidden array, first (which is arr[0]). Rebuild and return the whole array arr. The answer is always unique.
encoded, an array of length n − 1, and first, which is arr[0].
The hidden array arr, which has length n.
1 ^ 0 = 1, 0 ^ 2 = 2 and 2 ^ 1 = 3. These match encoded, and arr starts with first = 1.
Every neighbour pair XORs to the matching encoded value: 4 ^ 2 = 6, 2 ^ 0 = 2, 0 ^ 7 = 7 and 7 ^ 4 = 3.
- • 2 ≤ n ≤ 10^4
- • encoded.length = n − 1
- • 0 ≤ encoded[i] ≤ 10^5
- • 0 ≤ first ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each position, try candidate values until arr[i] ^ candidate equals encoded[i]
arr[i + 1] = arr[i] ^ encoded[i], one step per position
We do not know arr[i + 1], but we know its range. Every value in arr is below 2^B, where 2^B is larger than every input number. For each position, try the candidates v = 0, 1, 2, … until arr[i] ^ v equals encoded[i], then keep v as arr[i + 1].
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.