Reported September 2026
Teslabit manipulation

Swap Even and Odd Bits

Reported by candidates from Tesla's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Tesla OA. Under 2s to a working solution.
Founder's read

The edge case that kills the naive answer on this Tesla OA, reported September 2026, is the sign bit. Swap Even and Odd Bits looks like a five-minute warmup, but the input is an unsigned 32-bit value passed as a long, and one careless shift or mask gives you garbage or a negative number. It's a bit manipulation problem with one clean trick. If you know it, you finish fast. If you blank on the masks, StealthCoder is the invisible safety net running during the live assessment, reading the problem and handing you the solution.

The problem

Given an unsigned 32-bit integer n, swap every bit at an even position with the adjacent bit at the next odd position.
Bit positions are counted from the least significant bit, starting at position 0. Therefore, swap the pairs (0, 1), (2, 3), and so on through (30, 31).
Return the resulting unsigned 32-bit integer.

Function
swapEvenOddBits(n: long) → long

Examples
Example 1
n = 10
return = 5
The low four bits of 10 are 1010. Swapping positions (0, 1) and (2, 3) produces 0101, which equals 5.
Example 2
n = 23
return = 43
The low six bits of 23 are 010111. Swapping each adjacent pair produces 101011, which equals 43.
Example 3
n = 1
return = 2
The set bit at position 0 moves to position 1, so the result is 2.

Constraints
0 <= n <= 2^32 - 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two masks and two shifts. Take the even-position bits with 0x55555555, shift them left by 1. Take the odd-position bits with 0xAAAAAAAA, shift them right by 1. OR the two results. That's the whole algorithm, constant time. The pitfall is the width. Since n goes up to 2^32 - 1 and the function takes a long, you must keep the answer inside 32 bits. Mask the final result with 0xFFFFFFFF so the left shift of bit 30 into bit 31 doesn't spill into bit 32. In Java, write the hex literals as long values (0xAAAAAAAAL), or 0xAAAAAAAA turns into a negative int and sign-extends. In Python, mask at the end. Check your answer against n = 23, which should return 43. If the masks slip from memory mid-assessment, StealthCoder is the hedge that gives you the working version while you keep typing.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Swap Even and Odd Bits cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Tesla's OA.

Tesla reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Swap Even and Odd Bits FAQ

How hard is Swap Even and Odd Bits really?+

Easy once you've seen the mask trick. It's a few lines and constant time. The difficulty is purely recall of 0x55555555 and 0xAAAAAAAA and handling the 32-bit boundary. If you try to loop bit by bit, it works but takes longer and invites off-by-one errors.

What's the trick to solving it fast?+

Isolate even bits with 0x55555555, isolate odd bits with 0xAAAAAAAA. Shift the even ones left by 1, the odd ones right by 1, then OR them together. Mask the result with 0xFFFFFFFF to stay in 32 bits. No loops needed.

Why does the unsigned 32-bit constraint matter?+

Because n can reach 2^32 - 1, bit 31 can be set. In languages with signed ints, 0xAAAAAAAA is negative and shifts sign-extend. Use a long or a 64-bit type and mask with 0xFFFFFFFF so the result stays a clean unsigned value.

Which test cases should I check?+

Run the three given examples: 10 returns 5, 23 returns 43, 1 returns 2. Then try 0, which returns 0, and 2^32 - 1, which returns itself since all bits are set. Also try a value with bit 31 set, like 2^31, which should return 2^30.

How do I prepare for this in 48 hours?+

Spend an hour on bit manipulation basics: masks, shifts, AND/OR, and the difference between logical and arithmetic right shift. Write this solution once from memory in your OA language. Then glance at related bit problems like counting set bits and reversing bits.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Tesla.

OA at Tesla?
Invisible during screen share
Get it