Question
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).
toggle_flag(state: int, k: int) → int[5,1]out7[7,0]out6State 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.
Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.