Practice: Pow(x, n)
Problem: https://leetcode.com/problems/powx-n/
Recognition reminder: you raise x to an integer power n that can be negative, and the constraints rule out multiplying one factor at a time. A self-contained number routine with an O(log n) target is the math cue.
Before you start (the five-beat rhythm)
- Your three Pattern Cards for this week are already written.
- Name the pattern aloud (which of the three, and why) and write your approach as a plain-English comment before any code.
- Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
- If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
- Debrief in your commit message before moving to the next problem.
Your target
Fill these in yourself before you look at anyone else’s solution:
Target time complexity: ____
Target space complexity: ____
Fast exponentiation by squaring is O(log n) multiplications and O(1) space, versus the O(n) naive loop. Handle the negative exponent up front (x**(-n) is 1 / (x**n)), and know why reading the bits of n gives the log factor.
Float note
The expected answers are floats, and floating-point arithmetic is not exact, so the provided test compares with an absolute tolerance (abs(got - expected) < 1e-5) rather than ==. For example pow(2.1, 3) is about 9.261 but stored as 9.261000000000001. Do not try to make your output exactly equal; aim for the right value within tolerance.
Where your code goes
Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.
Debrief (paste into your commit message)
1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?