FastPrepMaximize Consecutive XOR
Problems/Maximize Consecutive XOR

Maximize Consecutive XOR

IBM logoIBM● MediumFULLTIMENEW GRADOA

Updated Aug 2026

QuestionFunctionExamplesConstraints

Problem statement

Given an integer n that is divisible by 4, find an integer x satisfying all of the following:

  1. x >= n.
  2. The binary representation of x has the same number of bits as the binary representation of n.
  3. The value v = n XOR (n + 1) XOR ... XOR x is as large as possible.

If several values of x maximize v, return the smallest such x.

Function

getMaxX(n: long) → long

Examples

Example 1

n = 4return = 6

The possible values are 4, 5, 6, and 7. Their consecutive XOR values are 4, 1, 7, and 0, respectively. The maximum is reached when x = 6.

Example 2

n = 8return = 14

The valid values have four binary bits. At x = 14, the range XOR equals 15, the largest four-bit value, so 14 is optimal.

Example 3

n = 16return = 30

The valid values have five binary bits. Choosing x = 30 makes the range XOR equal 31, which is the largest possible five-bit value.

Constraints

  • 4 <= n <= 10^12.
  • n is divisible by 4.

More IBM Online Assessment questions

  • Missing IntervalsOA · Seen Nov 2026
  • Count Peaks and ValleysOA · Seen Nov 2026
  • Last Object Exit TimeOA · Seen Nov 2026
  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
  • Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
  • Tree Pythagorean TriplesOA · Seen Oct 2026
  • Minimum Power-of-Two Removal OperationsOA · Seen Oct 2026
  • Maximum Compatible Time WindowsOA · Seen Oct 2026

Similar questions at other companies

  • Knights, Knaves and Their StatementsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
  • Repeated Mirror XORMathWorks · OAMathWorks interview questions
  • Minimum Power-of-Two Removal OperationsVisa · OAVisa interview questions
  • Minimum Operations To Reduce To ZeroUber · OAUber interview questions
  • City Infection NumberArcesium · OAArcesium interview questions
  • Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions
  • Total Set Bits from 1 to NPeopleStrong · OAPeopleStrong interview questions
  • Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
  • Find the Missing Number from 1 to nGEP · OA

IBM interview experiences

  • IBM Full-Time SWE OA Questions: 1 Experience
  • IBM Software Intern Résumé Rounds: 2 Experiences (Part 3 of 3)
  • IBM Intern Blitz Interviews: 23 Experiences (Part 2 of 3)
  • IBM Software Intern Interviews: 44 Experiences (Part 1 of 3)
  • IBM Software Intern Interview Questions: 15 Experiences
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public long getMaxX(long n) {
      // Write your code here.
    }

}
Checking account…