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 201

Bitwise AND of Numbers Range (LeetCode 201)

You get two integers, left and right, with left <= right. Take every integer from left up to right, including both ends. Combine all of them with the bitwise AND operator. Bitwise AND compares numbers bit by bit and keeps a bit only when every number has a 1 in that place.

Return that single result.

Worked Examples

Example 1
Input:left = 5, right = 7
Output:4
Explanation: The numbers are 5 (101), 6 (110) and 7 (111). The top bit is 1 in all three, and each of the two lower bits is 0 in at least one of them. So the AND is 100, which is 4.
Example 2
Input:left = 0, right = 0
Output:0
Explanation: The range holds only 0, so the AND is 0.
Example 3
Input:left = 1, right = 2147483647
Output:0
Explanation: The range contains 1 and 2. They have no 1 bit in common, so the AND of the whole range is 0.

⚖️Formal Constraints & Bounds

  • 0 <= left <= right <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

Counting up from left to right passes the number prefix followed by 0111...1 and then the number prefix followed by 1000...0. These two numbers have no 1 bit in common below the prefix, so the AND clears every lower bit. The answer is the shared prefix followed by zeros: shift both ends right until they are equal, count the shifts, and shift the prefix back left by that count.

Real-World Scenario & Production Applications

In network routing, the addresses of one subnet share their leading bits. The AND of all addresses in an aligned subnet is its network address: the shared leading bits followed by zeros.

Step-by-Step Execution Trace Table

Trace of Example 1: left = 5, right = 7, shift = 0.

Stepleftrightleft != rightshift
start101 (5)111 (7)yes0
after round 110 (2)11 (3)yes1
after round 21 (1)1 (1)no, stop2

The ends are equal after two rounds, so the shared prefix is 1. The function returns left << shift = 1 << 2 = 4.

Code Shape
Archetype Code Shape
Python
1shift = 0
2while left != right:
3 left >>= 1
4 right >>= 1
5 shift += 1
6return left << shift

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Write left and right in binary. Counting up from left to right changes the low bits many times, but the high bits that the two ends share never change. Any bit below the highest place where the ends differ is 0 in at least one number of the range, so the AND clears it. The answer is the shared prefix, followed by zeros.

➗ What Is Added To Bit Tricks

Bit Tricks moves the bits of one number with shifts and masks. Here two numbers are shifted together. The loop while left != right removes the lowest bit of both ends until they are equal, and shift counts the removed bits. The last line, left << shift, shifts the equal value back.

🔑 The Key Idea in the Code

After the loop, left holds only the shared prefix. The prefix must go back to its original place, so the answer is left << shift. Returning left alone gives a number that is 2 ** shift times too small.

  • Returning left without shifting it back: after the loop, left holds only the shared prefix. return left gives 1 for left = 5 and right = 7, where the answer is left << shift = 4.

  • Returning left & right of the two ends: a number between the ends can have a 0 where both ends have a 1. For 5 and 7, the number 6 clears bit 0, so the answer is 4, not 5.

  • Shifting only right in the loop: the loop ends when left equals right. Shifting only right moves it toward 0 while left stays, so the loop never ends when left is larger than 0.

  • Looping over every number of the range: the range can hold about 2 billion numbers, but the shift loop runs at most 31 times.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The bitwise AND of all numbers in the range" with right up to 231 - 1: the range can hold about 2 billion numbers, so a loop over the range is too slow, and the answer must come from the two ends alone.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

left and right are the original ends with their lowest shift bits removed, and their shared prefix is the shared prefix of the original ends. The loop stops when they are equal, so left is then the shared prefix, and left << shift restores its place.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

This is Shared Prefix. Counting up from left to right changes every bit below the highest place where the two ends differ, so the AND clears all of those bits. Only the binary prefix that the two ends share survives. I keep a counter, shift, at zero. While left is not equal to right, I shift both right by one and add one to shift. When they are equal, they hold exactly the shared prefix. The trap is the last line: left is only the prefix, so I return left shifted back left by shift, not left itself. For left 5 and right 7 the loop ends with left 1 and shift 2, and the answer is 4. Each round removes one bit from both ends, so the loop runs at most 31 times. That is O of log right time and O of 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log right)

Look at the loop. Each round shifts both ends right by one place, so right loses one bit per round. When right has no bits left, both ends are 0 and equal. right has at most 31 bits, so the loop runs at most 31 times. The number of rounds grows with the number of bits of right, which is log right, so time is O(log right).

SPACE COMPLEXITY

O(1)

The code keeps three variables, left, right and shift, whatever the size of the range. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T=log⁡2(right)=O(log⁡right)T = \log_2(\text{right}) = O(\log \text{right})T=log2​(right)=O(logright)

Derivation Progression

Rounds

≤31\le 31≤31

right has at most 31 bits and loses one per round, so at most 31 rounds run.

Work per round

3 = O(1)

Two shifts and one addition.

Total

3⋅31=O(log⁡right)3 \cdot 31 = O(\log \text{right})3⋅31=O(logright)

Rounds times a constant amount of work.

Variable Definitions

rightrightright

The larger end of the range, at most 2^31 - 1

shiftshiftshift

How many low bits have been removed from both ends

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): the variables left, right and shift

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1): the ends are already equal, so the loop does not run.

Average Case

O(log⁡right)O(\log \text{right})O(logright).

Worst Case

O(log⁡right)O(\log \text{right})O(logright): 31 rounds, for example left = 1 and right = 2147483647.

Pointer Invariant Transition Progression

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

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "bitwise AND of all numbers in this range", a range that can reach 231−12^{31} - 1231−1. Looping over the range is too slow, so the answer must be read from the two ends. Any bit that changes inside the range is cleared by the AND, so only the binary prefix that the ends share can remain.

CONSTRAINTS & BOUNDS

0≤left≤right≤231−10 \le \text{left} \le \text{right} \le 2^{31} - 10≤left≤right≤231−1, so the range can hold about 2 billion numbers. A loop with a running AND is O(right−left)O(\text{right} - \text{left})O(right−left). The shift loop runs at most 31 times, so it is O(log⁡right)O(\log \text{right})O(logright), and it uses only three variables.

FAANG PRODUCTION TRAPS & EDGE CASES

Returning left without << shift gives the prefix without its original place: left = 5, right = 7 returns 1 instead of 4. Returning left & right of the two ends keeps bits that a number inside the range clears: it returns 5 for the same input. In a fixed-width language, left << shift stays below 2312^{31}231, because it is at most the original left, so it does not overflow.

Theory Context•Bit Manipulation
MediumLC 201

Bitwise AND of Numbers Range (LeetCode 201)

You get two integers, left and right, with left <= right. Take every integer from left up to right, including both ends. Combine all of them with the bitwise AND operator. Bitwise AND compares numbers bit by bit and keeps a bit only when every number has a 1 in that place.

Return that single result.

Worked Examples

Example 1
Input:left = 5, right = 7
Output:4
Explanation: The numbers are 5 (101), 6 (110) and 7 (111). The top bit is 1 in all three, and each of the two lower bits is 0 in at least one of them. So the AND is 100, which is 4.
Example 2
Input:left = 0, right = 0
Output:0
Explanation: The range holds only 0, so the AND is 0.
Example 3
Input:left = 1, right = 2147483647
Output:0
Explanation: The range contains 1 and 2. They have no 1 bit in common, so the AND of the whole range is 0.

⚖️Formal Constraints & Bounds

  • 0 <= left <= right <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

Counting up from left to right passes the number prefix followed by 0111...1 and then the number prefix followed by 1000...0. These two numbers have no 1 bit in common below the prefix, so the AND clears every lower bit. The answer is the shared prefix followed by zeros: shift both ends right until they are equal, count the shifts, and shift the prefix back left by that count.

Real-World Scenario & Production Applications

In network routing, the addresses of one subnet share their leading bits. The AND of all addresses in an aligned subnet is its network address: the shared leading bits followed by zeros.

Step-by-Step Execution Trace Table

Trace of Example 1: left = 5, right = 7, shift = 0.

Stepleftrightleft != rightshift
start101 (5)111 (7)yes0
after round 110 (2)11 (3)yes1
after round 21 (1)1 (1)no, stop2

The ends are equal after two rounds, so the shared prefix is 1. The function returns left << shift = 1 << 2 = 4.

Code Shape
Archetype Code Shape
Python
1shift = 0
2while left != right:
3 left >>= 1
4 right >>= 1
5 shift += 1
6return left << shift

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Write left and right in binary. Counting up from left to right changes the low bits many times, but the high bits that the two ends share never change. Any bit below the highest place where the ends differ is 0 in at least one number of the range, so the AND clears it. The answer is the shared prefix, followed by zeros.

➗ What Is Added To Bit Tricks

Bit Tricks moves the bits of one number with shifts and masks. Here two numbers are shifted together. The loop while left != right removes the lowest bit of both ends until they are equal, and shift counts the removed bits. The last line, left << shift, shifts the equal value back.

🔑 The Key Idea in the Code

After the loop, left holds only the shared prefix. The prefix must go back to its original place, so the answer is left << shift. Returning left alone gives a number that is 2 ** shift times too small.

  • Returning left without shifting it back: after the loop, left holds only the shared prefix. return left gives 1 for left = 5 and right = 7, where the answer is left << shift = 4.

  • Returning left & right of the two ends: a number between the ends can have a 0 where both ends have a 1. For 5 and 7, the number 6 clears bit 0, so the answer is 4, not 5.

  • Shifting only right in the loop: the loop ends when left equals right. Shifting only right moves it toward 0 while left stays, so the loop never ends when left is larger than 0.

  • Looping over every number of the range: the range can hold about 2 billion numbers, but the shift loop runs at most 31 times.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The bitwise AND of all numbers in the range" with right up to 231 - 1: the range can hold about 2 billion numbers, so a loop over the range is too slow, and the answer must come from the two ends alone.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

left and right are the original ends with their lowest shift bits removed, and their shared prefix is the shared prefix of the original ends. The loop stops when they are equal, so left is then the shared prefix, and left << shift restores its place.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

This is Shared Prefix. Counting up from left to right changes every bit below the highest place where the two ends differ, so the AND clears all of those bits. Only the binary prefix that the two ends share survives. I keep a counter, shift, at zero. While left is not equal to right, I shift both right by one and add one to shift. When they are equal, they hold exactly the shared prefix. The trap is the last line: left is only the prefix, so I return left shifted back left by shift, not left itself. For left 5 and right 7 the loop ends with left 1 and shift 2, and the answer is 4. Each round removes one bit from both ends, so the loop runs at most 31 times. That is O of log right time and O of 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log right)

Look at the loop. Each round shifts both ends right by one place, so right loses one bit per round. When right has no bits left, both ends are 0 and equal. right has at most 31 bits, so the loop runs at most 31 times. The number of rounds grows with the number of bits of right, which is log right, so time is O(log right).

SPACE COMPLEXITY

O(1)

The code keeps three variables, left, right and shift, whatever the size of the range. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T=log⁡2(right)=O(log⁡right)T = \log_2(\text{right}) = O(\log \text{right})T=log2​(right)=O(logright)

Derivation Progression

Rounds

≤31\le 31≤31

right has at most 31 bits and loses one per round, so at most 31 rounds run.

Work per round

3 = O(1)

Two shifts and one addition.

Total

3⋅31=O(log⁡right)3 \cdot 31 = O(\log \text{right})3⋅31=O(logright)

Rounds times a constant amount of work.

Variable Definitions

rightrightright

The larger end of the range, at most 2^31 - 1

shiftshiftshift

How many low bits have been removed from both ends

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): the variables left, right and shift

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1): the ends are already equal, so the loop does not run.

Average Case

O(log⁡right)O(\log \text{right})O(logright).

Worst Case

O(log⁡right)O(\log \text{right})O(logright): 31 rounds, for example left = 1 and right = 2147483647.

Pointer Invariant Transition Progression

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

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "bitwise AND of all numbers in this range", a range that can reach 231−12^{31} - 1231−1. Looping over the range is too slow, so the answer must be read from the two ends. Any bit that changes inside the range is cleared by the AND, so only the binary prefix that the ends share can remain.

CONSTRAINTS & BOUNDS

0≤left≤right≤231−10 \le \text{left} \le \text{right} \le 2^{31} - 10≤left≤right≤231−1, so the range can hold about 2 billion numbers. A loop with a running AND is O(right−left)O(\text{right} - \text{left})O(right−left). The shift loop runs at most 31 times, so it is O(log⁡right)O(\log \text{right})O(logright), and it uses only three variables.

FAANG PRODUCTION TRAPS & EDGE CASES

Returning left without << shift gives the prefix without its original place: left = 5, right = 7 returns 1 instead of 4. Returning left & right of the two ends keeps bits that a number inside the range clears: it returns 5 for the same input. In a fixed-width language, left << shift stays below 2312^{31}231, because it is at most the original left, so it does not overflow.

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