Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Flip a Bit at Position
00:00
5 left

Flip a Bit at Position

EasyPython

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):
Interviewer

Your question is Flip a Bit at Position. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.