Magicsheet logo

Bitwise OR of Adjacent Elements

Easy
62.5%
Updated 8/1/2025

Asked by 1 Company

Bitwise OR of Adjacent Elements

What is this problem about?

The Bitwise OR of Adjacent Elements coding problem is a straightforward array manipulation exercise. Given an array of integers, you are tasked with creating a new array where each element at index i is the result of a bitwise OR operation between the elements at index i and i+1 of the original array. This problem is a great introduction to bitwise operators and simple array traversals.

Why is this asked in interviews?

Companies like Adobe use this as an introductory question for junior roles or internships. It tests your basic understanding of the | (OR) operator and your ability to handle array indexing correctly without going out of bounds. While simple, it ensures that a candidate can perform basic tasks efficiently before moving on to more complex algorithmic challenges.

Algorithmic pattern used

This problem follows a simple Simulation or Linear Traversal pattern. You iterate through the array from the first element up to the second-to-last element, performing the OR operation on the current element and its neighbor. The time complexity is O(N)O(N), and the space complexity is O(N)O(N) for the new array (or O(1)O(1) if the result is printed directly).

Example explanation

Suppose you have an array: [1, 2, 4, 8]

  • Index 0 and 1: 1 | 2 = 0001 | 0010 = 0011 (3)
  • Index 1 and 2: 2 | 4 = 0010 | 0100 = 0110 (6)
  • Index 2 and 3: 4 | 8 = 0100 | 1000 = 1100 (12) The resulting array is [3, 6, 12].

Common mistakes candidates make

The most common mistake is an "off-by-one" error in the loop range, which leads to an IndexOutOfBounds exception when trying to access i+1 for the last element. Another mistake is confusing the bitwise OR (|) with the logical OR (||) or the bitwise XOR (^).

Interview preparation tip

Always double-check your loop boundaries. When you're accessing i+1, your loop should generally run until length - 2. This is a small detail that shows attention to code safety.

Similar Questions