Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

13 Core Algorithmic Patterns & 225 Practice Problems

  • 1. Two Pointers (16 Paradigms, 43 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (feasible(x)), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 11 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (15 Paradigms, 24 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (5 Paradigms, 12 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (12 Paradigms, 22 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (13 Paradigms, 28 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (7 Paradigms, 17 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Stack (5 Paradigms, 9 Problems): Next greater element, largest rectangle in histogram, remove k digits, decode string, min stack.
  • 13. Intervals & Greedy (2 Paradigms, 6 Problems): Merge intervals, interval scheduling, meeting rooms by sweep line, jump game.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

Arena
238Items
Theory Context•Bit Manipulation
MediumLC 371

Sum of Two Integers (LeetCode 371)

You get two integers, a and b. Return their sum. The code may not use the + operator or the - operator anywhere.

The only tools left are the bitwise operators and shifts, which work on the binary digits of a number.

Worked Examples

Example 1
Input:a = 1, b = 2
Output:3
Explanation: 1 is 01 and 2 is 10. No place has a 1 in both numbers, so there is no carry, and the sum without carries is 11, which is 3.
Example 2
Input:a = 2, b = 3
Output:5
Explanation: 2 is 010 and 3 is 011. The sum without carries is 001. Both numbers have a 1 in place 1, which makes a carry into place 2. Adding the carry 100 gives 101, which is 5.

⚖️Formal Constraints & Bounds

  • -1000 <= a, b <= 1000

  • The operators + and - must not be used

Deep-Dive & Conceptual Insights

Core Insight

In binary addition, a place where only one number has a 1 gives a sum bit of 1, and a place where both numbers have a 1 gives a sum bit of 0 and a carry into the next place. a ^ b is every sum bit without carries, and (a & b) << 1 is every carry, already in the place where it belongs. Their sum is the answer, so repeat with the carries as the new second number. Each round moves every carry at least one place left, so within 32 rounds nothing is left to carry.

Real-World Scenario & Production Applications

A processor adds two numbers with a chain of small adders, one for each binary place. Each adder produces a sum bit with XOR and a carry bit with AND, and passes the carry to the next place on the left.

Step-by-Step Execution Trace Table

Trace of Example 2: a = 2, b = 3. Small numbers, so the 32-bit mask changes nothing.

Stepabtotal = a ^ bcarry = (a & b) << 1
start010 (2)011 (3)
round 1010 (2)011 (3)001 (1)100 (4)
round 2001 (1)100 (4)101 (5)000 (0)
end101 (5)000 (0)

After round 2, b is 0, so a = 5 is the sum.

A negative example: a = -5, b = -3. After the mask, a is 4294967291 and b is 4294967293. The loop ends with a = 4294967288, which has bit 31 set. The last line turns it into ~(4294967288 ^ 4294967295) = ~7 = -8.

Code Shape
Archetype Code Shape
Python
1a, b = a & mask, b & mask
2while b:
3 total = (a ^ b) & mask
4 carry = ((a & b) << 1) & mask
5 a, b = total, carry
6return a if a <= sign_max else ~(a ^ mask)

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Adding in binary works place by place. In a place where only one number has a 1, the sum is 1. Where both have a 1, the sum is 0 and a carry of 1 goes to the next place on the left. The XOR a ^ b is the sum of every place without the carries. The AND a & b marks the places that make a carry, and shifting it left by 1 moves each carry to the place where it belongs. Adding those two numbers is the same problem again, but the carries have moved one place to the left.

➗ What Is Added To Bit Tricks

Bit Tricks moves the bits of one number with shifts and masks. Here two numbers are combined with XOR and AND, and the loop while b repeats until the carries are gone. The mask keeps both numbers in 32 bits, and the last line turns the 32-bit pattern back into a signed number.

🔑 The Key Idea in the Code

Each round, every carry moves at least one place left. After at most 32 rounds the carries have left the 32-bit word, so b is 0 and a is the sum. A negative Python integer has no fixed width, so without mask the loop can run forever.

  • Using no 32-bit mask in Python: a negative Python integer has no fixed width, so for a = -1 and b = 1 the carry moves left forever and b never becomes 0. The loop never ends.

  • Not shifting the carry one place left: (a & b) without << 1 adds the carry back into the same place. For a = 2 and b = 3 the result is 3, where 5 is right.

  • Returning a without converting it back to a signed value: for a = -5 and b = -3 the result is 4294967288, where -8 is right.

  • Running one round instead of looping until b is 0: the carry of one round can cause another carry. For a = 2 and b = 3, one round returns 1, where 5 is right.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Sum of two integers without + or -": the operators for adding are not allowed, so the addition must be built from bit operators. XOR gives the sum of each place without carries, and AND shifted left gives the carries.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

a + b equals the original sum, modulo 2 ** 32, after every round. a ^ b plus (a & b) << 1 equals a + b, so setting a = total and b = carry keeps the sum. When b is 0, a is the sum.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • A carry that is not shifted left: 2 + 3 returns 3, because the carry is added into the same place it came from.

The 60-Second Interview Pitch

Say this out loud before you type a single line

This is Carry Add. Binary addition has two parts. The sum of each place without carries is a XOR b. The carries are a AND b, shifted left by one, because a carry goes to the next place. Their sum is the answer, so I repeat with the carries as the new b until b is 0. In Python I cut every value to 32 bits with a mask, because a negative Python integer has no fixed width and its carry would never leave. At the end I turn the 32-bit pattern back into a signed value: if it is above the largest signed value, I flip its bits and invert the result. For 2 and 3, round one gives 1 and 4, round two gives 5 and 0, and the answer is 5. Each round moves every carry at least one place left, so there are at most 32 rounds. That is O of 1 time and O of 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(1)

Look at the loop. Each round replaces b by the carries, and every carry sits at least one place to the left of the place that made it. After 32 rounds every carry is past bit 31, and mask removes it, so b is 0. The loop runs at most 32 times, whatever the values of a and b, so time is O(1). Written with the word size w, it is O(w).

SPACE COMPLEXITY

O(1)

The code keeps six integers, mask, sign_max, a, b, total and carry, whatever the input. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T≤w=32=O(1)T \le w = 32 = O(1)T≤w=32=O(1)

Derivation Progression

Rounds

≤32\le 32≤32

Every carry moves at least one place left per round and leaves the word after bit 31.

Work per round

5 = O(1)

One XOR, one AND, one shift and two masks.

Total

5⋅32=O(1)5 \cdot 32 = O(1)5⋅32=O(1)

Rounds times a constant amount of work.

Variable Definitions

www

The word width, 32 bits

carrycarrycarry

The places where both numbers have a 1, moved one place left

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): six integers

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1): one of the numbers is 0, so the loop does not run.

Average Case

O(1)O(1)O(1).

Worst Case

O(1)O(1)O(1): 32 rounds, for example a = -1 and b = 1.

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

The prompt asks for the "sum of two integers without using + and -". Binary addition works place by place: a ^ b is the sum of every place without carries, and (a & b) << 1 holds the carries, each moved one place to the left. Repeating the step with the carries as the new second number adds them in.

CONSTRAINTS & BOUNDS

−1000≤a,b≤1000-1000 \le a, b \le 1000−1000≤a,b≤1000. Each round moves every carry at least one place to the left, so the carries leave a 32-bit word after at most 32 rounds. Time is O(1)O(1)O(1) and space is O(1)O(1)O(1).

FAANG PRODUCTION TRAPS & EDGE CASES

Without mask, a negative Python integer never loses its carry: -1 + 1 never ends. Without << 1, the carry is added back into the same place: 2 + 3 returns 3. Without the last line, -5 + -3 returns 4294967288 instead of -8.

Theory Context•Bit Manipulation
MediumLC 371

Sum of Two Integers (LeetCode 371)

You get two integers, a and b. Return their sum. The code may not use the + operator or the - operator anywhere.

The only tools left are the bitwise operators and shifts, which work on the binary digits of a number.

Worked Examples

Example 1
Input:a = 1, b = 2
Output:3
Explanation: 1 is 01 and 2 is 10. No place has a 1 in both numbers, so there is no carry, and the sum without carries is 11, which is 3.
Example 2
Input:a = 2, b = 3
Output:5
Explanation: 2 is 010 and 3 is 011. The sum without carries is 001. Both numbers have a 1 in place 1, which makes a carry into place 2. Adding the carry 100 gives 101, which is 5.

⚖️Formal Constraints & Bounds

  • -1000 <= a, b <= 1000

  • The operators + and - must not be used

Deep-Dive & Conceptual Insights

Core Insight

In binary addition, a place where only one number has a 1 gives a sum bit of 1, and a place where both numbers have a 1 gives a sum bit of 0 and a carry into the next place. a ^ b is every sum bit without carries, and (a & b) << 1 is every carry, already in the place where it belongs. Their sum is the answer, so repeat with the carries as the new second number. Each round moves every carry at least one place left, so within 32 rounds nothing is left to carry.

Real-World Scenario & Production Applications

A processor adds two numbers with a chain of small adders, one for each binary place. Each adder produces a sum bit with XOR and a carry bit with AND, and passes the carry to the next place on the left.

Step-by-Step Execution Trace Table

Trace of Example 2: a = 2, b = 3. Small numbers, so the 32-bit mask changes nothing.

Stepabtotal = a ^ bcarry = (a & b) << 1
start010 (2)011 (3)
round 1010 (2)011 (3)001 (1)100 (4)
round 2001 (1)100 (4)101 (5)000 (0)
end101 (5)000 (0)

After round 2, b is 0, so a = 5 is the sum.

A negative example: a = -5, b = -3. After the mask, a is 4294967291 and b is 4294967293. The loop ends with a = 4294967288, which has bit 31 set. The last line turns it into ~(4294967288 ^ 4294967295) = ~7 = -8.

Code Shape
Archetype Code Shape
Python
1a, b = a & mask, b & mask
2while b:
3 total = (a ^ b) & mask
4 carry = ((a & b) << 1) & mask
5 a, b = total, carry
6return a if a <= sign_max else ~(a ^ mask)

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Adding in binary works place by place. In a place where only one number has a 1, the sum is 1. Where both have a 1, the sum is 0 and a carry of 1 goes to the next place on the left. The XOR a ^ b is the sum of every place without the carries. The AND a & b marks the places that make a carry, and shifting it left by 1 moves each carry to the place where it belongs. Adding those two numbers is the same problem again, but the carries have moved one place to the left.

➗ What Is Added To Bit Tricks

Bit Tricks moves the bits of one number with shifts and masks. Here two numbers are combined with XOR and AND, and the loop while b repeats until the carries are gone. The mask keeps both numbers in 32 bits, and the last line turns the 32-bit pattern back into a signed number.

🔑 The Key Idea in the Code

Each round, every carry moves at least one place left. After at most 32 rounds the carries have left the 32-bit word, so b is 0 and a is the sum. A negative Python integer has no fixed width, so without mask the loop can run forever.

  • Using no 32-bit mask in Python: a negative Python integer has no fixed width, so for a = -1 and b = 1 the carry moves left forever and b never becomes 0. The loop never ends.

  • Not shifting the carry one place left: (a & b) without << 1 adds the carry back into the same place. For a = 2 and b = 3 the result is 3, where 5 is right.

  • Returning a without converting it back to a signed value: for a = -5 and b = -3 the result is 4294967288, where -8 is right.

  • Running one round instead of looping until b is 0: the carry of one round can cause another carry. For a = 2 and b = 3, one round returns 1, where 5 is right.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Sum of two integers without + or -": the operators for adding are not allowed, so the addition must be built from bit operators. XOR gives the sum of each place without carries, and AND shifted left gives the carries.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

a + b equals the original sum, modulo 2 ** 32, after every round. a ^ b plus (a & b) << 1 equals a + b, so setting a = total and b = carry keeps the sum. When b is 0, a is the sum.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • A carry that is not shifted left: 2 + 3 returns 3, because the carry is added into the same place it came from.

The 60-Second Interview Pitch

Say this out loud before you type a single line

This is Carry Add. Binary addition has two parts. The sum of each place without carries is a XOR b. The carries are a AND b, shifted left by one, because a carry goes to the next place. Their sum is the answer, so I repeat with the carries as the new b until b is 0. In Python I cut every value to 32 bits with a mask, because a negative Python integer has no fixed width and its carry would never leave. At the end I turn the 32-bit pattern back into a signed value: if it is above the largest signed value, I flip its bits and invert the result. For 2 and 3, round one gives 1 and 4, round two gives 5 and 0, and the answer is 5. Each round moves every carry at least one place left, so there are at most 32 rounds. That is O of 1 time and O of 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(1)

Look at the loop. Each round replaces b by the carries, and every carry sits at least one place to the left of the place that made it. After 32 rounds every carry is past bit 31, and mask removes it, so b is 0. The loop runs at most 32 times, whatever the values of a and b, so time is O(1). Written with the word size w, it is O(w).

SPACE COMPLEXITY

O(1)

The code keeps six integers, mask, sign_max, a, b, total and carry, whatever the input. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T≤w=32=O(1)T \le w = 32 = O(1)T≤w=32=O(1)

Derivation Progression

Rounds

≤32\le 32≤32

Every carry moves at least one place left per round and leaves the word after bit 31.

Work per round

5 = O(1)

One XOR, one AND, one shift and two masks.

Total

5⋅32=O(1)5 \cdot 32 = O(1)5⋅32=O(1)

Rounds times a constant amount of work.

Variable Definitions

www

The word width, 32 bits

carrycarrycarry

The places where both numbers have a 1, moved one place left

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): six integers

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1): one of the numbers is 0, so the loop does not run.

Average Case

O(1)O(1)O(1).

Worst Case

O(1)O(1)O(1): 32 rounds, for example a = -1 and b = 1.

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

The prompt asks for the "sum of two integers without using + and -". Binary addition works place by place: a ^ b is the sum of every place without carries, and (a & b) << 1 holds the carries, each moved one place to the left. Repeating the step with the carries as the new second number adds them in.

CONSTRAINTS & BOUNDS

−1000≤a,b≤1000-1000 \le a, b \le 1000−1000≤a,b≤1000. Each round moves every carry at least one place to the left, so the carries leave a 32-bit word after at most 32 rounds. Time is O(1)O(1)O(1) and space is O(1)O(1)O(1).

FAANG PRODUCTION TRAPS & EDGE CASES

Without mask, a negative Python integer never loses its carry: -1 + 1 never ends. Without << 1, the carry is added back into the same place: 2 + 3 returns 3. Without the last line, -5 + -3 returns 4294967288 instead of -8.

© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•