PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's understanding of bitwise operations and low-level integer manipulation, along with the ability to reason about the time and space complexity of bit-level operations.

  • Medium
  • Box
  • Coding & Algorithms
  • Software Engineer

Flip a specific bit in an integer

Company: Box

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given a non-negative integer num and a zero-based bit position p, return the integer resulting from flipping only the bit at position p (i.e., 0 becomes 1, 1 becomes 0). Explain your approach and its time/space complexity.

Quick Answer: This question evaluates a candidate's understanding of bitwise operations and low-level integer manipulation, along with the ability to reason about the time and space complexity of bit-level operations.

Given a non-negative integer `num` and a zero-based bit position `p`, return the integer that results from flipping ONLY the bit at position `p` (a `0` becomes `1`, a `1` becomes `0`). All other bits remain unchanged. The bit at position `p` has place value 2^p. Flipping it is a classic application of the XOR operator: XOR-ing any bit with `1` toggles it, while XOR-ing with `0` leaves it unchanged. So building a mask with a single `1` at position `p` (`1 << p`) and XOR-ing it with `num` flips exactly that one bit. Example: `num = 5` (binary `101`), `p = 1`. The mask `1 << 1` is `010`. `101 ^ 010 = 111 = 7`. Return the resulting integer.

Constraints

  • 0 <= num (non-negative integer)
  • 0 <= p (zero-based bit position)
  • Flipping a bit at a position beyond num's highest set bit simply sets that bit to 1.
  • Python integers are arbitrary-precision, so very large p values are handled natively.

Examples

Input: (5, 0)

Expected Output: 4

Explanation: 5 is 101. Bit 0 is 1, so it clears to 0: 100 = 4.

Input: (5, 1)

Expected Output: 7

Explanation: 5 is 101. Bit 1 is 0, so it sets to 1: 111 = 7.

Input: (0, 0)

Expected Output: 1

Explanation: 0 is 0. Flipping bit 0 sets it: 1.

Input: (8, 3)

Expected Output: 0

Explanation: 8 is 1000. Bit 3 is the only set bit; flipping it clears the number to 0.

Input: (0, 31)

Expected Output: 2147483648

Explanation: Flipping bit 31 of 0 yields 2^31 = 2147483648, showing high-bit positions work.

Input: (1023, 5)

Expected Output: 991

Explanation: 1023 is 1111111111. Clearing bit 5 (value 32) gives 1023 - 32 = 991.

Input: (7, 10)

Expected Output: 1031

Explanation: 7 is 111. Bit 10 is beyond the top set bit, so flipping it just adds 2^10 = 1024: 7 + 1024 = 1031.

Hints

  1. Which bitwise operator toggles a bit: AND, OR, or XOR? Recall that x ^ 1 = NOT x and x ^ 0 = x.
  2. Build a mask with a single 1 at position p using a left shift: 1 << p.
  3. XOR the number with that mask: num ^ (1 << p). This flips only the targeted bit and leaves every other bit untouched, in O(1) time and space.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Leaky Bucket Rate Limiter - Box (easy)
  • Solve classic troubleshooting & algorithm tasks - Box (Medium)
  • Compute Top-K word frequencies under a path - Box (Medium)
  • Design out-of-order windowed stream processor - Box (Medium)