Start by clarifying edge cases (e.g., n <= 0) and then present a bitwise solution using n > 0 && (n & (n-1)) == 0. Explain why this works and discuss its O(1) time and space complexity, contrasting it with iterative division or logarithm approaches.
Pro tip: Mention that the bitwise trick is not only efficient but also a common pattern in low-level programming and system design, showing you understand its practical relevance beyond interviews.
Ask about constraints: can n be negative, zero, or non-integer? Confirm that a power of two means n = 2^k for some integer k >= 0.
Briefly mention iterative division by 2 or using logarithms, noting their time complexity (O(log n)) and potential floating-point issues.
Explain that a power of two has exactly one bit set, so n & (n-1) clears the lowest set bit. If n > 0 and n & (n-1) == 0, it's a power of two.
State O(1) time and space. Handle n <= 0 explicitly (return false). Test with examples: 1, 2, 4, 8 (true); 0, -2, 3, 6 (false).
Summarize the solution and offer to discuss alternative approaches or related problems (e.g., power of three).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.