Practice: Sum of Two Integers
Problem: https://leetcode.com/problems/sum-of-two-integers/
Recognition reminder: you add two integers without using + or -, which forces you to think about addition at the level of bits and carries. That is the bit cue, and in Python it carries the fixed-width wrinkle the note below covers.
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: ____
The loop runs at most a constant number of times (bounded by the bit width), so O(1) time and O(1) space. The interesting cost here is correctness in Python, not speed: read the note below before you start.
A Python-specific subtlety (read this before you code)
This problem is specified in 32-bit terms, and the standard bit trick relies on fixed-width overflow that Python does not have. In C or Java, the carry (a & b) << 1 eventually shifts off the 32-bit edge and the value wraps to a negative number through two’s complement, which is how the loop terminates and how negative results appear. In Python, integers are arbitrary precision: nothing ever falls off, so a naive while b != 0: loop can run forever (the carry just keeps growing), and a result that should be negative is never represented as a wrapped bit pattern.
You must impose the 32-bit width yourself:
- Mask with
& 0xFFFFFFFFeach step to keep only the low 32 bits, and loop while the masked carry is nonzero. - At the end, if the result’s bit 31 (the sign bit) is set, the true value is negative; reinterpret it by hand (for example,
~(result ^ 0xFFFFFFFF)), otherwise return it as is.
The provided expected values are (1, 2) -> 3, (-2, 3) -> 1, (-1, 1) -> 0. If your loop hangs or your negative cases come out as huge positive numbers, the mask is what you are missing. This wrinkle is exactly the “bit tricks are not width-agnostic” misconception from this week’s README, made concrete.
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?