Toggle feature flag bit
A feature-flag service stores a rollout state as a non-negative integer, one flag per bit. An admin clicks a switch in the UI, which must flip flag k: if bit k is 0 it becomes 1, and if it is 1 it becomes 0, leaving every other bit untouched. Given the current state (0 <= state < 2^31) and the flag index k (0 <= k <= 30), return the new state. For example, state 5 (binary 101) with k = 1 returns 7 (binary 111).
Implement
toggle_flag(state: int, k: int) → intExamples
in
[5,1]out7in
[7,0]out6What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 10 min
solution.py
InputExpectedGot
[5,1]7not run yetsample[7,0]6not run yetsample