Bit Manipulation - Power of FourIdentify the bug in the following code snippet that attempts to check if a number is a power of four:AUsing 'log_val == int(log_val)' causes false negatives due to floating point precisionBThe condition 'if n <= 0' should be 'if n < 0' to allow zeroCThe division by 2 should be multiplication by 0.5 for better precisionDThe function should use a loop instead of logarithm for accuracyCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand floating point precision issueFloating point calculations can introduce tiny errors, so direct equality check can fail for valid powers of four.Step 2: Identify the bugThe line 'return log_val == int(log_val)' is the bug because it does not allow for a tolerance margin.Final Answer:Option A -> Option AQuick Check:Adding a tolerance check like abs(log_val - int(log_val)) < epsilon fixes the bug [OK]Quick Trick: Floating point equality without tolerance causes subtle bugsCommon Mistakes:MISTAKESIgnoring floating point errorsMisusing inequality conditions for input validationTrap Explanation:PITFALLCandidates often trust direct equality on floats, missing precision issues that cause rare failures.Interviewer Note:CONTEXTChecks candidate's awareness of floating point pitfalls in mathematical checks.
Master "Power of Four" in Bit Manipulation3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Bit Manipulation Quizzes Counting Bits (0 to N) - Counting Bits (0 to N) - Quiz 15hard Counting Bits (0 to N) - Counting Bits (0 to N) - Quiz 14medium Generate All Permutations Using Bitmask DP - Generate All Permutations Using Bitmask DP - Quiz 5medium Number of 1 Bits (Hamming Weight) - Number of 1 Bits (Hamming Weight) - Quiz 15hard Number of 1 Bits (Hamming Weight) - Number of 1 Bits (Hamming Weight) - Quiz 6medium Number of 1 Bits (Hamming Weight) - Number of 1 Bits (Hamming Weight) - Quiz 3easy Reverse Bits - Reverse Bits - Quiz 14medium Single Number II (Three Copies) - Single Number II (Three Copies) - Quiz 13medium Subsets Using Bitmask - Subsets Using Bitmask - Quiz 5medium Sum of Two Integers Without + Operator - Sum of Two Integers Without + Operator - Quiz 10hard