Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flip a Bit at Position

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Flip a Bit at Position. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Box services may represent compact file or folder attributes as a 32-bit unsigned integer. Given a packed flags value and a zero-based bit position, flip the bit at that position and return the updated integer.

Flipping means changing 0 to 1 or 1 to 0. All other bits must remain unchanged. Use bitwise operations rather than converting the number to a string or scanning its binary representation.

Formal Specification

Implement flip_bit(flags, position):

  • flags is a non-negative 32-bit integer.
  • position is an integer from 0 through 31, where position 0 is the least significant bit.
  • Return a non-negative 32-bit integer containing the updated flags.

The input is always valid, and the function must not mutate external state.

Constraints

  • 0 <= flags <= 2^32 - 1
  • 0 <= position <= 31
  • The result fits in an unsigned 32-bit integer
  • Input values are valid, so explicit error handling is not required

Function Signature

def flip_bit(flags, position):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output