Quick Overview

A bit manipulation problem: given a 32-bit unsigned integer, swap each even-position nibble, one hexadecimal digit of 4 bits, with the adjacent odd-position nibble and return the result. It tests masks, shifts and handling values above the signed 32-bit range.

Swap Adjacent Nibble Pairs in a 32-Bit Unsigned Integer

Company: Arista Networks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

A nibble is a group of 4 bits, which is one hexadecimal digit. Given a 32-bit unsigned integer, swap every even-position nibble with the odd-position nibble next to it, and return the result. For example, the hexadecimal value `0x12345678` becomes `0x21436587`. The report did not state the integer's width; this version fixes it at 32 bits. ### Function Signature ```python def swap_nibbles(x: int) -> int: ``` ### Rules - Nibble positions count from the least significant end: position 0 is bits 0 to 3, position 1 is bits 4 to 7, and so on up to position 7, which is bits 28 to 31. - Swap position 0 with position 1, 2 with 3, 4 with 5, and 6 with 7. - `x` is treated as exactly 32 bits, so leading zero nibbles take part in the swap: `0x1` becomes `0x10`. - Return the result as a non-negative integer in the same 32-bit range. Inputs and outputs are plain integers; the hexadecimal forms below are only for reading. ### Constraints - `0 <= x <= 2^32 - 1`, that is, `0 <= x <= 4294967295` - Inputs and outputs can exceed the 32-bit signed maximum `2^31 - 1`, so in a fixed-width language use an unsigned 32-bit or a 64-bit type. ### Examples **Example 1** ```text Input: x = 305419896 (0x12345678) Output: 558065031 (0x21436587) ``` The pairs `12`, `34`, `56` and `78` each swap their two digits. **Example 2** ```text Input: x = 171 (0x000000AB) Output: 186 (0x000000BA) ``` Nibble 0 holds `B` and nibble 1 holds `A`; they trade places. The zero nibbles above them swap with each other and stay zero. **Example 3** ```text Input: x = 4026531841 (0xF0000001) Output: 251658256 (0x0F000010) ``` Nibbles 7 and 6 (`F` and `0`) swap, and nibbles 1 and 0 (`0` and `1`) swap.

Overview: A bit manipulation problem: given a 32-bit unsigned integer, swap each even-position nibble, one hexadecimal digit of 4 bits, with the adjacent odd-position nibble and return the result. It tests masks, shifts and handling values above the signed 32-bit range.

Read the full Arista Networks Software Engineer interview experience this question came from

A nibble is a group of 4 bits, which is one hexadecimal digit. You are given an unsigned 32-bit integer `x`. Swap every even-position nibble with the odd-position nibble next to it, and return the resulting integer. **Rules** - Nibble positions count from the least significant end: position 0 is bits 0 to 3, position 1 is bits 4 to 7, and so on up to position 7, which is bits 28 to 31. - Swap position 0 with position 1, position 2 with position 3, position 4 with position 5, and position 6 with position 7. - `x` is treated as exactly 32 bits, so leading zero nibbles take part in the swap: `0x1` becomes `0x10`. - Return the result as a non-negative integer in the same 32-bit range. Inputs and outputs are plain integers; the hexadecimal forms below are only for reading. **Example 1** ```text Input: x = 305419896 (0x12345678) Output: 558065031 (0x21436587) ``` The pairs `12`, `34`, `56` and `78` each swap their two digits. **Example 2** ```text Input: x = 4026531841 (0xF0000001) Output: 251658256 (0x0F000010) ``` Nibbles 7 and 6 (`F` and `0`) swap, and nibbles 1 and 0 (`0` and `1`) swap. **Constraints** - `0 <= x <= 2^32 - 1`, that is, `0 <= x <= 4294967295` - Inputs and outputs can exceed the 32-bit signed maximum `2^31 - 1` (2147483647), so Java uses `long` and C++ uses `long long`. Every value stays below 2^53, so a JavaScript number holds it exactly.

Constraints

  • 0 <= x <= 2^32 - 1, that is, 0 <= x <= 4294967295
  • x is treated as exactly 32 bits (8 nibbles), so leading zero nibbles take part in the swap
  • Inputs and outputs can exceed 2^31 - 1 = 2147483647: Java uses long and C++ uses long long; all values stay below 2^53

Examples

Input: (0,)

Expected Output: 0

Explanation: Minimum value: all eight nibbles are zero, so swapping changes nothing.

Input: (1,)

Expected Output: 16

Explanation: 0x00000001 becomes 0x00000010: the leading-zero nibble 1 swaps in with nibble 0.

Hints

  1. Number the eight nibbles 0 to 7 from the least significant end; each even position trades places with the odd position directly above it.
  2. Leading zero nibbles still count: picture x as always having exactly eight hexadecimal digits.
  3. The result can be as large as 4294967295, so make sure your return type and intermediate values cannot overflow or turn negative.

Loading coding console...

Show the approach

Approach

Algorithm: the mask 0x0F0F0F0F keeps exactly the even-position nibbles (0, 2, 4, 6) and the mask 0xF0F0F0F0 keeps exactly the odd-position nibbles (1, 3, 5, 7). Shifting the even nibbles left by 4 bits moves each one into the odd slot directly above it, and shifting the odd nibbles right by 4 bits moves each one into the even slot directly below it. The two shifted parts occupy disjoint bit positions, so OR-ing them gives the swapped value.

Invariant and correctness: after masking, every kept nibble moves exactly one nibble position, and the destination of every even nibble is its own pair partner, never a neighbouring pair, because even nibble 2k lands on 2k + 1 and odd nibble 2k + 1 lands on 2k. Nibble 6 (the highest even one) shifts into nibble 7, so nothing leaves bit 31, and nibble 1 (the lowest odd one) shifts into nibble 0, so nothing is shifted out below bit 0. The result therefore stays within 0 to 4294967295 without a further mask.

Edge cases: x = 0 and values whose pairs hold equal nibbles (0x33333333, 0xFFFFFFFF) are fixed points. Leading zero nibbles take part naturally because the masks always cover all 32 bits, so 0x1 becomes 0x10. Values can exceed 2^31 - 1, so Java and C++ use 64-bit types; JavaScript bitwise operators work on signed 32-bit values, so its reference uses an unsigned right shift (>>> 4) for the odd half and converts the combined result back to an unsigned value with >>> 0.

Time complexity:
O(1)
Space complexity:
O(1)