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•Binary Search Boundary
EasyLC 69

Sqrt(x) (LC 69)

Target Frequency:AppleAmazonGoogle

Take a whole number x that is 0 or more. Find the largest whole number whose square is at most x: the square root of x with any fractional part thrown away. For x = 8, whose real root is about 2.83, that number is 2.

The problem forbids letting a built-in exponent function or operator do the work, so something like x ** 0.5 in Python or pow(x, 0.5) in C++ is not allowed.

Worked Examples

Example 1
Input:x = 4
Output:2
k
00112233answer
k * k <= 4
True0True1True2False3
Explanation: 2 * 2 = 4 fits exactly and 3 * 3 = 9 is too big, so the answer is 2.
Example 2
Input:x = 8
Output:2
k
001122334455answer
k * k <= 8
True0True1True2False3False4False5
Explanation: The real root of 8 is about 2.83. 2 * 2 = 4 fits, but 3 * 3 = 9 is already more than 8, so rounding down gives 2.

⚖️Formal Constraints & Bounds

  • 0 <= x <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

The rounded-down root is the last k with k * k <= x. Keep a passing mid with lo = mid and discard a failing mid with hi = mid - 1; round the midpoint up to guarantee progress.

Real-World Scenario & Production Applications

Integer-only code, such as firmware on chips without floating-point hardware or fixed-point graphics, needs an exact integer square root: the side of the largest square grid that fits in x cells, or comparing distances without floats. Python ships this exact function as math.isqrt.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Check and Range

k * k <= x is True, then False. Search candidates [0, x + 1).

Mathematical Recurrence / Code Invariant
def fits(k): return k * k <= x

Step-by-Step Execution Trace Table

For x = 8, pass [0, 9); shift to lo = -1, hi = 8. Probe 4 (False): hi = 3. Probe 1 (True): lo = 1. Probe 2 (True): lo = 2. Probe 3 (False): hi = 2. Return 2. At lo = 2, hi = 3, a floor midpoint would probe 2 and stall; ceiling probes 3.

Code Shape
Archetype Code Shape
Python
1def fits(k):
2 return k * k <= x
3lo = 0
4hi = x + 1
5return binary_search_upper_bound(lo, hi, fits)

Conceptual Narrative

The rounded-down square root is the largest nonnegative k whose square fits in x. The check k * k <= x reads True, then False, so use Upper Bound to find the last True.

Pass [0, x + 1) to the helper. It shifts both endpoints down by 1 and uses the ceiling midpoint. A passing mid becomes lo; a failing mid makes hi = mid - 1. The answer stays in [lo, hi], and both updates shrink that range.

For x = 8 the answer is 2. A floor midpoint can stall at lo = 2, hi = 3; the ceiling midpoint probes 3 and discards it.

  • Floor midpoint: With lo = mid, a floor midpoint can equal lo and stall. Round up with + 1.

  • Keeping a failing mid: Use hi = mid - 1; hi = mid can leave hi unchanged.

  • Shifting twice: Call the helper with [0, x + 1); it shifts the bounds internally.

  • Position versus index: bisect_right returns the first greater position, not the last True index.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The largest whole number whose square is at most x" is a last True: the check k * k <= x gets harder as k grows, so it runs True, ..., True, False, ..., False. "No built-in exponent function or operator" rules out x ** 0.5. The largest value that still passes such a check is Upper Bound, here over the candidates 0 to x.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

The check k * k <= x is True up to the answer and False after it. Shift lo and hi down by 1, then use mid = lo + (hi - lo + 1) // 2. Keep a passing mid; discard a failing mid.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Use hi = mid - 1 when the check fails; hi = mid can stall.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I use Upper Bound to find the largest k whose square fits in x. I pass [0, x + 1), and the helper shifts both endpoints down by 1. A ceiling midpoint stays strictly above lo. A passing mid becomes lo; a failing mid makes hi = mid - 1. The last True stays between the bounds, and at equality I return lo. The floor midpoint can stall with lo = mid. This takes O(log(x + 1)) time and O(1) space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log X)

Look at the code: lo, hi = 0, x + 1 starts with X + 1 candidate roots. The Upper Bound template halves the range on every pass of while lo < hi, and each pass calls the check once: one square and one comparison. So there are about log2(X + 1) passes, and the cost is O(log X). The generic blueprint's O(T * log N) becomes O(log X), because T is O(1) here and N = X + 1. A floating-point sqrt would be O(1), but the problem forbids built-in exponent functions such as pow(x, 0.5), so O(log X) integer steps is the bound to aim for here.

SPACE COMPLEXITY

O(1)

The code keeps lo, hi and mid (plus the small helper is_feasible), whatever X is, and returns one integer. There is no recursion and nothing is stored per candidate, so the extra space is O(1).

Formal Recurrence Relation

T(X) = T(X / 2) + O(1) = O(log X)

Derivation Progression

Setup

O(1)

lo = 0 and hi = x + 1 are constant work.

Halving loop

about log2(X + 1) passes

Each pass of while lo < hi keeps [mid, hi] or [lo, mid - 1], reducing the number of possible answers by about half.

One probe

O(1) per pass

One midpoint, one mid * mid, one comparison with x, and one bounds update.

Total

O(log X)

Constant work times a logarithmic number of passes: 31 passes at X = 231 - 1.

Variable Definitions

XXX

The input x, from 0 to 2^31 - 1

midmidmid

The candidate root being tested by the Upper Bound loop, lo + (hi - lo + 1) // 2

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative, no recursion

🔵 Auxiliary Heap

O(1): lo, hi, mid

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(log⁡X)O(\log X)O(logX): there is no early exit, so every x of the same size takes about the same number of passes (tiny inputs also terminate without special cases)

Average Case

O(log⁡X)O(\log X)O(logX)

Worst Case

O(log⁡X)O(\log X)O(logX): the largest x needs the most passes

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: the answer is the largest whole number whose square is at most x, and no built-in exponent function or operator may compute it. The largest value that still passes a check that only gets harder is a last True, so the move is Upper Bound over the candidate roots 0 to x. The card shifts the half-open bounds internally and uses a ceiling midpoint to preserve the last True.

CONSTRAINTS & BOUNDS

0≤x≤231−10 \le x \le 2^{31} - 10≤x≤231−1, so there are about 2312^{31}231 candidate roots. Budget: O(log⁡x)O(\log x)O(logx) time, O(1)O(1)O(1) space. In Java, C# or C++, mid * mid reaches about 1.15×10181.15 \times 10^{18}1.15×1018: square in a 64-bit integer, where it fits, or compare mid == 0 or mid <= x / mid (the division is exact when mid >= 1, and mid = 0 always fits).

FAANG PRODUCTION TRAPS & EDGE CASES

Using a floor midpoint with lo = mid can stall. With lo = 2 and hi = 3, it probes 2 and leaves lo unchanged; the ceiling midpoint probes 3. Floating-point shortcuts such as int(math.sqrt(x)) happen to be exact for 32-bit inputs, but for 64-bit inputs a double's 53-bit mantissa cannot hold every integer and the result can round the wrong way; exact integer code searches instead.

Theory Context•Binary Search Boundary
EasyLC 69

Sqrt(x) (LC 69)

Target Frequency:AppleAmazonGoogle

Take a whole number x that is 0 or more. Find the largest whole number whose square is at most x: the square root of x with any fractional part thrown away. For x = 8, whose real root is about 2.83, that number is 2.

The problem forbids letting a built-in exponent function or operator do the work, so something like x ** 0.5 in Python or pow(x, 0.5) in C++ is not allowed.

Worked Examples

Example 1
Input:x = 4
Output:2
k
00112233answer
k * k <= 4
True0True1True2False3
Explanation: 2 * 2 = 4 fits exactly and 3 * 3 = 9 is too big, so the answer is 2.
Example 2
Input:x = 8
Output:2
k
001122334455answer
k * k <= 8
True0True1True2False3False4False5
Explanation: The real root of 8 is about 2.83. 2 * 2 = 4 fits, but 3 * 3 = 9 is already more than 8, so rounding down gives 2.

⚖️Formal Constraints & Bounds

  • 0 <= x <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

The rounded-down root is the last k with k * k <= x. Keep a passing mid with lo = mid and discard a failing mid with hi = mid - 1; round the midpoint up to guarantee progress.

Real-World Scenario & Production Applications

Integer-only code, such as firmware on chips without floating-point hardware or fixed-point graphics, needs an exact integer square root: the side of the largest square grid that fits in x cells, or comparing distances without floats. Python ships this exact function as math.isqrt.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Check and Range

k * k <= x is True, then False. Search candidates [0, x + 1).

Mathematical Recurrence / Code Invariant
def fits(k): return k * k <= x

Step-by-Step Execution Trace Table

For x = 8, pass [0, 9); shift to lo = -1, hi = 8. Probe 4 (False): hi = 3. Probe 1 (True): lo = 1. Probe 2 (True): lo = 2. Probe 3 (False): hi = 2. Return 2. At lo = 2, hi = 3, a floor midpoint would probe 2 and stall; ceiling probes 3.

Code Shape
Archetype Code Shape
Python
1def fits(k):
2 return k * k <= x
3lo = 0
4hi = x + 1
5return binary_search_upper_bound(lo, hi, fits)

Conceptual Narrative

The rounded-down square root is the largest nonnegative k whose square fits in x. The check k * k <= x reads True, then False, so use Upper Bound to find the last True.

Pass [0, x + 1) to the helper. It shifts both endpoints down by 1 and uses the ceiling midpoint. A passing mid becomes lo; a failing mid makes hi = mid - 1. The answer stays in [lo, hi], and both updates shrink that range.

For x = 8 the answer is 2. A floor midpoint can stall at lo = 2, hi = 3; the ceiling midpoint probes 3 and discards it.

  • Floor midpoint: With lo = mid, a floor midpoint can equal lo and stall. Round up with + 1.

  • Keeping a failing mid: Use hi = mid - 1; hi = mid can leave hi unchanged.

  • Shifting twice: Call the helper with [0, x + 1); it shifts the bounds internally.

  • Position versus index: bisect_right returns the first greater position, not the last True index.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The largest whole number whose square is at most x" is a last True: the check k * k <= x gets harder as k grows, so it runs True, ..., True, False, ..., False. "No built-in exponent function or operator" rules out x ** 0.5. The largest value that still passes such a check is Upper Bound, here over the candidates 0 to x.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

The check k * k <= x is True up to the answer and False after it. Shift lo and hi down by 1, then use mid = lo + (hi - lo + 1) // 2. Keep a passing mid; discard a failing mid.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Use hi = mid - 1 when the check fails; hi = mid can stall.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I use Upper Bound to find the largest k whose square fits in x. I pass [0, x + 1), and the helper shifts both endpoints down by 1. A ceiling midpoint stays strictly above lo. A passing mid becomes lo; a failing mid makes hi = mid - 1. The last True stays between the bounds, and at equality I return lo. The floor midpoint can stall with lo = mid. This takes O(log(x + 1)) time and O(1) space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log X)

Look at the code: lo, hi = 0, x + 1 starts with X + 1 candidate roots. The Upper Bound template halves the range on every pass of while lo < hi, and each pass calls the check once: one square and one comparison. So there are about log2(X + 1) passes, and the cost is O(log X). The generic blueprint's O(T * log N) becomes O(log X), because T is O(1) here and N = X + 1. A floating-point sqrt would be O(1), but the problem forbids built-in exponent functions such as pow(x, 0.5), so O(log X) integer steps is the bound to aim for here.

SPACE COMPLEXITY

O(1)

The code keeps lo, hi and mid (plus the small helper is_feasible), whatever X is, and returns one integer. There is no recursion and nothing is stored per candidate, so the extra space is O(1).

Formal Recurrence Relation

T(X) = T(X / 2) + O(1) = O(log X)

Derivation Progression

Setup

O(1)

lo = 0 and hi = x + 1 are constant work.

Halving loop

about log2(X + 1) passes

Each pass of while lo < hi keeps [mid, hi] or [lo, mid - 1], reducing the number of possible answers by about half.

One probe

O(1) per pass

One midpoint, one mid * mid, one comparison with x, and one bounds update.

Total

O(log X)

Constant work times a logarithmic number of passes: 31 passes at X = 231 - 1.

Variable Definitions

XXX

The input x, from 0 to 2^31 - 1

midmidmid

The candidate root being tested by the Upper Bound loop, lo + (hi - lo + 1) // 2

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative, no recursion

🔵 Auxiliary Heap

O(1): lo, hi, mid

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(log⁡X)O(\log X)O(logX): there is no early exit, so every x of the same size takes about the same number of passes (tiny inputs also terminate without special cases)

Average Case

O(log⁡X)O(\log X)O(logX)

Worst Case

O(log⁡X)O(\log X)O(logX): the largest x needs the most passes

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: the answer is the largest whole number whose square is at most x, and no built-in exponent function or operator may compute it. The largest value that still passes a check that only gets harder is a last True, so the move is Upper Bound over the candidate roots 0 to x. The card shifts the half-open bounds internally and uses a ceiling midpoint to preserve the last True.

CONSTRAINTS & BOUNDS

0≤x≤231−10 \le x \le 2^{31} - 10≤x≤231−1, so there are about 2312^{31}231 candidate roots. Budget: O(log⁡x)O(\log x)O(logx) time, O(1)O(1)O(1) space. In Java, C# or C++, mid * mid reaches about 1.15×10181.15 \times 10^{18}1.15×1018: square in a 64-bit integer, where it fits, or compare mid == 0 or mid <= x / mid (the division is exact when mid >= 1, and mid = 0 always fits).

FAANG PRODUCTION TRAPS & EDGE CASES

Using a floor midpoint with lo = mid can stall. With lo = 2 and hi = 3, it probes 2 and leaves lo unchanged; the ceiling midpoint probes 3. Floating-point shortcuts such as int(math.sqrt(x)) happen to be exact for 32-bit inputs, but for 64-bit inputs a double's 53-bit mantissa cannot hold every integer and the result can round the wrong way; exact integer code searches instead.

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