{"id":"sqrtx","title":"Sqrt(x)","difficulty":"easy","patterns":["binary-search"],"primaryConcept":"binary-search-basics","concepts":["binary-search-basics"],"problemForm":"intro","companies":["Amazon","Google","Microsoft","Meta"],"category":"searching-sorting","executionStyle":"solution-method","compareMode":"exact","syntheticTestConfig":{"inputShape":"scalar","primaryParam":"x","sizeMetric":"scalar","sizes":[8,64,1024,1048576]},"description":"Given a non-negative integer `x`, return the square root of `x` rounded down to the nearest integer.\n\nDo not use a built-in exponent function or operator.","examples":[{"input":"x = 4","output":"2","explanation":"The square root of 4 is exactly 2."},{"input":"x = 8","output":"2","explanation":"The square root of 8 is about 2.828, so the rounded-down integer is 2."},{"input":"x = 1","output":"1","explanation":"The square root of 1 is exactly 1."}],"constraints":[{"id":"0-x-2-31-1","text":"0 <= x <= 2^31 - 1"}],"starterCode":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        # Your code here\n        pass","functionName":"mySqrt","testCases":[{"id":"1","input":{"x":4},"expected":2,"description":"Perfect square."},{"id":"2","input":{"x":8},"expected":2,"description":"Non-perfect square rounds down."},{"id":"3","input":{"x":0},"expected":0,"description":"Zero input."},{"id":"4","input":{"x":1},"expected":1,"description":"One input."},{"id":"5","input":{"x":2147395599},"expected":46339,"description":"Large value just below a perfect square."},{"id":"h1","input":{"x":2},"expected":1,"description":"Small non-perfect square."},{"id":"h2","input":{"x":16},"expected":4,"description":"Another perfect square."},{"id":"h3","input":{"x":2147483647},"expected":46340,"description":"Maximum 32-bit signed integer."},{"id":"h4","input":{"x":15},"description":"A small non-square rounds down.","expected":3},{"id":"h5","input":{"x":2147395600},"description":"An exact large square at the 32-bit edge.","expected":46340}],"hints":[{"level":1,"content":"The answer is between 0 and x, and the predicate mid * mid <= x is monotonic.","revealPattern":false},{"level":2,"content":"Binary search for the largest integer whose square does not exceed x.","revealPattern":true},{"level":3,"content":"Use `mid = left + (right - left) // 2`, then compare `mid * mid` with `x`.","revealPattern":true}],"guidedFlow":{"version":2,"progressRevision":"sqrtx-2026-07-18-binary-search-bridge","approachSelection":{"contract":"approach-selection-v1","question":"Which approach should we implement?","decisionPrompt":"Choose the approach that avoids floating point and returns the floor for non-perfect squares.","approaches":[{"id":"binary-search-floor","name":"Binary search floor root","summary":"Search integer candidates and keep narrowing around the largest valid square.","decisionReason":"It is exact for integers, fast for large x, and naturally returns the floor when no exact square exists.","complexity":{"time":"O(log x)","space":"O(1)"},"tradeoffs":["Requires careful boundary updates, but avoids precision issues."],"isOptimal":true,"steps":["Compare mid² with x.","Move right when mid is valid.","Move left when mid is too large.","Return the final right boundary."]},{"id":"linear-increment","name":"Try every integer","summary":"Increase a candidate until its square exceeds x.","decisionReason":"This is simple but wastes the monotonic structure of the search space.","complexity":{"time":"O(sqrt(x))","space":"O(1)"},"tradeoffs":["Easy implementation, slower as x grows."],"isOptimal":false},{"id":"floating-point","name":"Use floating-point sqrt","summary":"Call a built-in square-root or exponent operation and truncate the result.","decisionReason":"This dodges the algorithmic reasoning and can introduce precision concerns.","complexity":{"time":"O(1)","space":"O(1)"},"tradeoffs":["Short code, but not the intended binary-search practice."],"isOptimal":false}]},"codingCheckpoints":[{"id":"init-bounds","title":"Initialize search bounds","description":"Start the candidate root range at 0 through x.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"initializes_search_bounds","scope":"init-pointers","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Starting left at 1 and mishandling x = 0","Using an unrelated upper bound"],"hint":"Use left = 0 and right = x.","semanticSpec":{"intent":"Initialize the binary-search bounds over every possible integer root.","doneWhen":"The lower bound starts at 0 and the upper bound starts at x before the search loop.","shouldFailWhen":"This step should fail if either bound is missing, off by one, or unrelated to x.","roles":["left boundary","right boundary","candidate root range"],"mustProve":["The search includes 0 as a possible root.","The search includes x as the initial upper bound."],"mustNotAccept":["A range that starts at 1 and loses x = 0.","An upper bound unrelated to the input."],"proofSnippets":[{"id":"py-sqrtx-init-bounds","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x"}]}},{"id":"search-window","title":"Maintain the search window","description":"Continue while the candidate interval is non-empty, and stop as soon as the bounds cross.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"maintains_search_window","scope":"while-condition","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Using a loop that never narrows the range","Stopping before checking the final candidate"],"hint":"The termination condition follows the interval: loop while left <= right, then use the crossed-boundary state.","semanticSpec":{"intent":"Maintain an inclusive search window until the bounds cross.","doneWhen":"The loop condition is controlled by the active left and right search bounds.","shouldFailWhen":"This step should fail if the loop ignores either bound or stops before the final candidate.","roles":["search window","loop guard"],"mustProve":["The loop guard compares the active search bounds."],"mustNotAccept":["A fixed-count loop.","A loop controlled by a midpoint or square value instead of the bounds."],"proofSnippets":[{"id":"py-sqrtx-search-window","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            pass"}]}},{"id":"compute-mid","title":"Compute midpoint safely","description":"Derive mid from the current bounds each iteration.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"computes_midpoint_from_bounds","scope":"calculate-mid","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Using left + right / 2 without parentheses","Not recalculating mid inside the loop"],"hint":"Compute the midpoint from the current bounds in an overflow-safe way so each probe still splits the active interval.","semanticSpec":{"intent":"Compute a midpoint from the current search bounds.","doneWhen":"Each iteration derives mid from both active bounds.","shouldFailWhen":"This step should fail if mid is constant, outside the loop, or based on only one bound.","roles":["left boundary","right boundary","midpoint root candidate"],"mustProve":["The midpoint is computed inside the search loop.","The midpoint uses both bounds."],"mustNotAccept":["A fixed probe value.","Linear advancement without a midpoint."],"proofSnippets":[{"id":"py-sqrtx-compute-mid","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2"}]}},{"id":"compare-square","title":"Compare the midpoint safely","description":"Decide whether mid is exact, too small, or too large using a comparison that preserves the square-vs-x ordering without overflowing fixed-width integers.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"branches_on_midpoint_boundary_predicate","scope":"check-boundary-predicate","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Comparing mid directly to x","Using a square calculation that can overflow in fixed-width integer languages"],"hint":"Use any overflow-safe comparison that still separates the three cases: below x, equal to x, or above x.","semanticSpec":{"intent":"Branch on a midpoint-derived comparison against x.","doneWhen":"The code compares the midpoint square or equivalent quotient-safe value with x.","shouldFailWhen":"This step should fail if the branch is not tied to the midpoint candidate.","roles":["midpoint root candidate","square comparison","target input"],"mustProve":["The branch predicate depends on the midpoint.","The predicate compares the midpoint-derived value with x."],"mustNotAccept":["A branch comparing mid directly to x.","A branch unrelated to the midpoint probe."],"proofSnippets":[{"id":"py-sqrtx-compare-square","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                pass\n            if square < x:\n                pass"}]}},{"id":"return-exact-square","title":"Return exact square hits","description":"When mid squared equals x, return mid immediately.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"returns_search_hit","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Ignoring perfect squares","Returning square instead of mid"],"hint":"If square == x, return mid.","semanticSpec":{"intent":"Return the midpoint immediately when it is an exact square root.","doneWhen":"The exact-match branch returns the midpoint candidate.","shouldFailWhen":"This step should fail if the exact branch returns the square value or falls through.","roles":["exact-match branch","midpoint root candidate","return value"],"mustProve":["The return is guarded by the exact midpoint comparison.","The returned value is the midpoint candidate."],"mustNotAccept":["Returning square instead of mid.","Ignoring exact square matches."],"proofSnippets":[{"id":"py-sqrtx-return-exact-square","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid"}]}},{"id":"narrow-bounds","title":"Narrow around mid","description":"Update the active bounds according to the comparison result while preserving the last candidate that could still be the floor root.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"narrows_bounds_around_midpoint","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Forgetting the +1 or -1 update","Moving the wrong bound for square < x"],"hint":"On any non-perfect square, the too-small branch must keep the candidate recoverable and the too-large branch must exclude the rejected candidate.","semanticSpec":{"intent":"Narrow the bounds according to whether the midpoint is too small or too large.","doneWhen":"The too-small branch advances the lower bound past mid and the too-large branch lowers the upper bound below mid.","shouldFailWhen":"This step should fail if either bound moves the wrong direction or fails to exclude mid.","roles":["comparison branch","left boundary","right boundary","midpoint root candidate"],"mustProve":["The branch on the midpoint comparison controls both bound updates.","The lower bound advances after a too-small midpoint.","The upper bound retreats after a too-large midpoint."],"mustNotAccept":["Reversed bound updates.","Updates that can leave the same midpoint in the window."],"proofSnippets":[{"id":"py-sqrtx-narrow-bounds","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid\n            if square < x:\n                left = mid + 1\n            else:\n                right = mid - 1"}]}},{"id":"return-floor","title":"Return the floor root","description":"After the bounds cross on a non-perfect square, return the last candidate that was not too large.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"returns_search_boundary","scope":"return-boundary","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Returning left after it has advanced past the answer","Returning -1 for non-perfect squares"],"hint":"Check an unseen non-square such as x = 15: after termination, right should be 3 because 4 has just been proven too large.","semanticSpec":{"intent":"Return the floor root from the crossed-boundary state.","doneWhen":"After the search loop exits, the code returns the boundary that still represents the last non-too-large candidate.","shouldFailWhen":"This step should fail if the code returns the advanced lower bound or a sentinel for non-perfect squares.","roles":["crossed search boundary","floor root candidate","return value"],"mustProve":["The terminal return occurs after the search loop.","The returned boundary is the floor-root side of the crossed bounds."],"mustNotAccept":["Returning left after it crossed past the floor.","Returning -1 for valid non-perfect squares."],"proofSnippets":[{"id":"py-sqrtx-return-floor","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid\n            if square < x:\n                left = mid + 1\n            else:\n                right = mid - 1\n        return right"}]}}],"complexityAnalysis":{"time":{"complexity":"O(log x)","explanation":"Each midpoint comparison discards about half of the remaining candidate roots."},"space":{"complexity":"O(1)","explanation":"Only a few integer variables are stored."}},"slides":[{"id":"opener","chapter":"The slow way","kind":"info","eyebrow":"Understand the task","title":"Floor the square root, by hand","workspacePanel":"problem","body":["Given a non-negative integer x, return the square root rounded down to a whole number, using only integer arithmetic. No floating-point sqrt.","So the answer is the largest integer r with r * r <= x. Nothing about that says how to find r without trying candidates, which is where the work is."]},{"id":"brute-force","chapter":"The slow way","kind":"code-trace","eyebrow":"The obvious way","title":"Try every candidate from zero up","summary":"This scan returns the floor root, but it checks candidates one at a time.","workspacePanel":"trace","codeTrace":{"contract":"code-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"r = 0\nwhile (r + 1) * (r + 1) <= x:\n    r = r + 1\nreturn r","steps":[{"id":"scan-0","title":"Start at candidate 0","summary":"x = 8. (0 + 1) squared is 1, which is at most 8, so 1 still fits and r climbs to 1.","codeLine":2,"state":{"nums":[0,1,2,3,4,5,6,7,8],"current":0,"x":8,"labels":{"nums":"candidate","current":"candidate"}}},{"id":"scan-1","title":"Candidate 1 still fits","summary":"(1 + 1) squared is 4, at most 8, so r climbs to 2. The scan only ever steps up by one.","codeLine":3,"state":{"nums":[0,1,2,3,4,5,6,7,8],"current":1,"x":8,"labels":{"nums":"candidate","current":"candidate"}}},{"id":"scan-2","title":"Candidate 2 is the last that fits","summary":"(2 + 1) squared is 9, which is greater than 8, so 3 overshoots. The loop stops and r stays 2.","codeLine":2,"state":{"nums":[0,1,2,3,4,5,6,7,8],"current":2,"x":8,"labels":{"nums":"candidate","current":"candidate"}}},{"id":"scan-return","title":"Return candidate 2","summary":"2 squared is 4 and 3 squared is 9, so 2 is the largest root whose square stays under 8. Three checks for an answer of 2.","codeLine":4,"state":{"nums":[0,1,2,3,4,5,6,7,8],"current":2,"x":8,"labels":{"nums":"candidate","current":"candidate"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let r = 0;\nwhile (((r + 1) * (r + 1)) <= x) {\n    r = r + 1;\n}\nreturn r;","lineMap":{"1":1,"2":2,"3":3,"4":5},"statementBundles":{"3":{"startLine":3,"endLine":4}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let r = 0;\nwhile (((r + 1) * (r + 1)) <= x) {\n    r = r + 1;\n}\nreturn r;","lineMap":{"1":1,"2":2,"3":3,"4":5},"statementBundles":{"3":{"startLine":3,"endLine":4}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int r = 0;\nwhile (((r + 1) * (r + 1)) <= x) {\n    r = r + 1;\n}\nreturn r;","lineMap":{"1":1,"2":2,"3":3,"4":5},"statementBundles":{"3":{"startLine":3,"endLine":4}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int r = 0;\nwhile ((r + 1) * (r + 1) <= x) {\n    r = r + 1;\n}\nreturn r;","lineMap":{"1":1,"2":2,"3":3,"4":5},"statementBundles":{"3":{"startLine":3,"endLine":4}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int r = 0;\nwhile (((r + 1) * (r + 1)) <= x) {\n    r = r + 1;\n}\nreturn r;","lineMap":{"1":1,"2":2,"3":3,"4":5},"statementBundles":{"3":{"startLine":3,"endLine":4}}}}},"body":["Start a candidate at 0 and keep raising it while the next value still squares to at most x. The last candidate that fits is the floor root."]},{"id":"brute-force-cost","chapter":"The slow way","kind":"case-probe","eyebrow":"Count the steps","title":"How many candidates can this scan check?","summary":"x can be as large as 2^31 - 1, just over two billion.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"worst-case-scan","state":{"x":2147483647,"candidate":12000,"step size":1,"stops near":"sqrt(x)"}}]},"options":[{"id":"up-to-sqrt","label":"Up to about sqrt(x) candidates, one per step","rationale":"The scan climbs one candidate per step until the answer, so a large x runs the loop roughly sqrt(x) times. For x near two billion that is about 46,340 squarings.","isCorrect":true},{"id":"up-to-x","label":"Up to x candidates","rationale":"The loop stops at the floor root, not at x. It runs about sqrt(x) times, far fewer than x, but still grows with the size of x.","isCorrect":false},{"id":"constant","label":"A fixed handful, since squares grow fast","rationale":"Squares grow fast, but the step size is always one, so the number of steps tracks sqrt(x) and is not bounded by a constant.","isCorrect":false}],"answer":"About sqrt(x) steps. For the largest allowed x that is over forty thousand one-at-a-time squarings. To improve on that, each square comparison has to avoid leaving nearly every candidate for the next step."},{"id":"monotonic-probe","chapter":"The trick","kind":"case-probe","eyebrow":"Spend one test well","title":"When a candidate's square overshoots x, what else is ruled out?","summary":"Candidates 0 through 8 for x = 8. The one being tested is 4, and 4 * 4 = 16 overshoots.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"overshoot","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":8,"mid":4,"x":8,"labels":{"nums":"candidate","left":"","right":"","mid":"tested"}}}]},"options":[{"id":"all-above","label":"Every candidate above 4 is ruled out too","rationale":"Squaring only grows as the candidate grows, so if 4 * 4 already passes 8, then 5, 6, 7, and 8 square even higher. One test discards the whole upper block.","isCorrect":true},{"id":"only-four","label":"Only candidate 4 is ruled out","rationale":"Larger candidates square to larger values, so a candidate that overshoots proves every bigger one overshoots as well.","isCorrect":false},{"id":"nothing-yet","label":"Nothing, until a candidate fits exactly","rationale":"An overshoot already deletes a whole side. The squares are sorted by candidate, so each test settles a block rather than a single value.","isCorrect":false}],"answer":"Because squares only grow with the candidate, 4 * 4 over 8 proves 5, 6, 7, 8 all overshoot too. One test clears half the range instead of one candidate."},{"id":"keep-floor-probe","chapter":"The trick","kind":"case-probe","eyebrow":"Handle a candidate that fits","title":"When a candidate's square fits under x, can you discard it?","summary":"Candidates 0 through 3 remain for x = 8. The one being tested is 1, and 1 * 1 = 1 sits under 8.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"fits","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":3,"mid":1,"x":8,"labels":{"nums":"candidate","mid":"tested","left":"","right":""}}}]},"options":[{"id":"keep-it","label":"No, keep it: it might still be the answer","rationale":"Candidate 1 fits, so the floor root is 1 or larger. A bigger candidate might also fit, but until one is proven to overshoot, 1 stays a possible answer and the search moves up without dropping it.","isCorrect":true},{"id":"drop-below","label":"Yes, discard 1 and everything at or below it","rationale":"Discarding 1 assumes a larger candidate fits, but nothing has proven that yet. 1 is the best root known so far and must survive until something bigger fits.","isCorrect":false},{"id":"return-now","label":"Return 1 right away, since it fits","rationale":"Fitting is not enough; 2 also fits for x = 8. The answer is the largest candidate that fits, so the search has to keep looking upward.","isCorrect":false}],"answer":"A candidate that fits is the best root so far, so keep it and search the larger side: the floor root is at this candidate or above it."},{"id":"midpoint-bridge","chapter":"The trick","kind":"case-probe","eyebrow":"Choose the probe","title":"Which candidate should the search test first?","summary":"Take a wider range than x = 8 for this one, candidates 0 through 15, so the halving divides evenly. You do not know whether the tested candidate will fit or overshoot, so minimise the larger remainder.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"choose-balanced-probe","state":{"nums":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"left":0,"right":15,"labels":{"nums":"candidate","left":"","right":""}}}]},"options":[{"id":"probe-middle","label":"Candidate 8","rationale":"If 8 overshoots, candidates 0 through 7 remain. If 8 fits, candidates 8 through 15 remain. Either result leaves at most eight possible roots.","isCorrect":true},{"id":"probe-near-start","label":"Candidate 1","rationale":"If 1 fits, candidates 1 through 15 can still contain the floor root. That outcome removes only candidate 0 and leaves fifteen possibilities.","isCorrect":false},{"id":"probe-end","label":"Candidate 15","rationale":"If 15 overshoots, candidates 0 through 14 remain. That outcome removes only candidate 15 and leaves fifteen possibilities.","isCorrect":false}],"answer":"Test candidate 8. An overshoot keeps 0 through 7, while a fit keeps 8 through 15, so either result cuts the sixteen candidates down to eight."},{"id":"name-binary-search","chapter":"The trick","kind":"case-probe","eyebrow":"Name the approach","title":"Which approach keeps only one half after each square comparison?","summary":"Still on those sixteen candidates: the tested candidate 8 leaves either 0 through 7 or 8 through 15.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"identify-binary-search","state":{"nums":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"left":0,"right":15,"mid":8,"labels":{"nums":"candidate","left":"","right":"","mid":"tested"}}}]},"options":[{"id":"binary-search-candidates","label":"Binary search the candidates","rationale":"Binary search tests the midpoint. Whether 8 fits or overshoots determines which eight-candidate half can still contain the floor root.","isCorrect":true},{"id":"linear-scan-candidates","label":"Scan upward one candidate at a time","rationale":"After testing one new candidate, an upward scan advances by one. It does not use the midpoint result to remove eight candidates at once.","isCorrect":false},{"id":"two-ended-scan-candidates","label":"Check both ends of the candidates","rationale":"Checking 0 and 15 settles those endpoints. Candidates 1 through 14 still remain, so the interval shrinks by two instead of half.","isCorrect":false}],"answer":"Use binary search on the candidate roots. A midpoint square comparison identifies the half that can still contain the largest candidate whose square does not exceed x."},{"id":"approach-bridge","chapter":"The trick","kind":"info","eyebrow":"Name the moves","title":"Halve the range, keep the largest that fits","summary":"Two moves carry the whole search.","ctaLabel":"Trace it","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"bridge","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":8,"x":8,"labels":{"nums":"candidate"}}}]},"body":["Track an untested window from left through right, starting with 0 through x. Every candidate below left is already known to fit; every candidate above right is known to overshoot.","Probe the middle candidate, mid, and square it. An overshoot moves right below mid; a fit moves left above it. If the square equals x, that candidate is the floor root. When the untested window empties, right separates the candidates that fit from those that overshoot."]},{"id":"trace","chapter":"Trace","kind":"dry-run","eyebrow":"Trace the state","title":"Trace x = 8 to its floor root","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"probe-4","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":8,"mid":4,"x":8,"labels":{"nums":"candidate"}}},{"id":"probe-1","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":3,"mid":1,"x":8,"labels":{"nums":"candidate"}}},{"id":"probe-2","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":2,"right":3,"mid":2,"x":8,"labels":{"nums":"candidate"}}},{"id":"cross","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":3,"right":2,"x":8,"labels":{"nums":"candidate"}}}]},"beats":[{"id":"probe-4","figure":"setup","title":"Where does right go after squaring candidate 4?","summary":"One overshoot is about to delete the whole upper half in a single step.","prompt":"The window is the whole range 0 to 8, the midpoint candidate is 4, and 4 * 4 = 16 overshoots 8.","options":[{"id":"right-to-3","label":"right moves to 3","rationale":"16 passes 8, so 4 and every larger candidate overshoot. The answer is below 4, and the window becomes 0 through 3.","isCorrect":true},{"id":"left-to-5","label":"left moves to 5","rationale":"That would search above 4, but 4 already overshoots, so nothing at or above it can be the floor root.","isCorrect":false},{"id":"right-to-4","label":"right moves to 4, keeping it","rationale":"Candidate 4 overshoots, so it cannot be the answer and must be excluded, not kept.","isCorrect":false}],"answer":"right becomes 3. 4 * 4 overshoots 8, so 4 and everything above it are gone, and the window is 0 through 3."},{"id":"probe-1","figure":"setup","title":"Where does left go after squaring candidate 1?","summary":"A candidate that fits has to survive, even as the search climbs past it.","prompt":"The window is 0 through 3, the midpoint candidate is 1, and 1 * 1 = 1 fits under 8.","options":[{"id":"left-to-2","label":"left moves to 2","rationale":"1 fits, so the floor root is 1 or higher. The search moves up to look for a larger candidate that also fits, narrowing the window to 2 through 3.","isCorrect":true},{"id":"right-to-1","label":"right moves to 1, keeping only 0 and 1","rationale":"1 fits under 8, so the answer is at least 1; cutting down to 0 and 1 throws away the larger candidates that might also fit.","isCorrect":false},{"id":"return-1","label":"Return 1 now","rationale":"2 also fits for x = 8, so 1 is not yet the largest fitting candidate; the search has to continue upward.","isCorrect":false}],"answer":"left becomes 2. 1 fits, so it stays a possible answer while the search climbs to test the larger candidates 2 and 3."},{"id":"probe-2","figure":"setup","title":"After candidate 2 fits, what is left about to do?","summary":"The last fitting candidate is about to push the window shut.","prompt":"The window is 2 through 3, the midpoint candidate is 2, and 2 * 2 = 4 fits under 8.","options":[{"id":"left-to-3","label":"left moves to 3, leaving only candidate 3","rationale":"2 fits, so left advances past it to 3. The window holds only 3 now, which the next probe will test and reject.","isCorrect":true},{"id":"right-to-2","label":"right moves to 2, leaving only candidate 2","rationale":"A fitting candidate moves the lower bound up. Since 2 fits, the search climbs rather than capping here.","isCorrect":false},{"id":"stop-at-2","label":"The search stops and returns 2","rationale":"Candidate 3 has not been tested yet, so the search cannot stop; it must climb to check whether 3 also fits.","isCorrect":false}],"answer":"left becomes 3. 2 fits, so the lower bound advances past it, and only candidate 3 remains to be tested."},{"id":"cross","figure":"setup","title":"The bounds crossed at left = 3, right = 2: what is returned?","summary":"Candidate 3 was just tested: 3 * 3 = 9 overshoots 8, so right dropped to 2 and the bounds crossed.","prompt":"The search is about to return after the window emptied.","options":[{"id":"return-right","label":"right, which is 2","rationale":"right holds the last candidate proven to fit. 3 overshot and was excluded, so right rests on 2, the largest candidate whose square stayed under 8.","isCorrect":true},{"id":"return-left","label":"left, which is 3","rationale":"left moved up chasing larger candidates and ended on 3, but the answer is the surviving lower boundary, which is right.","isCorrect":false},{"id":"return-mid","label":"the last midpoint","rationale":"The last midpoint was 3, which overshot. The answer is the surviving boundary, not wherever the final probe happened to land.","isCorrect":false}],"answer":"Return right, which is 2. Every overshoot pushed right down to the last candidate that fit, so right rests on the floor root."}]},{"id":"return-left-pitfall","chapter":"Trace","kind":"pitfall","eyebrow":"Spot the trap","title":"After the loop, could we return left instead?","summary":"left and right end one apart, so returning left when the loop exits looks just as defensible as returning right. It even passes every perfect square, where the loop returns mid before the bounds ever cross. Non-perfect squares are where the two choices part ways.","workspacePanel":"trace","pitfall":"The code below returns left after the loop instead of right.","whyItMatters":"Return right. When the bounds cross, left has advanced onto a candidate just proven to overshoot, while right holds the last candidate that fit, so right is the floor root.","negativeTrace":{"contract":"negative-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"left, right = 0, x\nwhile left <= right:\n    mid = left + (right - left) // 2\n    square = mid * mid\n    if square == x:\n        return mid\n    if square < x:\n        left = mid + 1\n    else:\n        right = mid - 1\nreturn left","steps":[{"id":"pf-probe-1","title":"Candidate 1 fits, left climbs","summary":"x = 2, window 0 through 2. mid = 1 and 1 * 1 = 1 fits under 2, so left moves up to 2.","codeLine":8,"state":{"nums":[0,1,2],"left":0,"right":2,"mid":1,"x":2,"labels":{"nums":"candidate"}}},{"id":"pf-probe-2","title":"Candidate 2 overshoots, right drops","summary":"Now the window is just candidate 2. mid = 2 and 2 * 2 = 4 overshoots 2, so right drops to 1 and the bounds cross.","codeLine":10,"state":{"nums":[0,1,2],"left":2,"right":2,"mid":2,"x":2,"labels":{"nums":"candidate"}}},{"id":"pf-wrong-return","title":"left returns one too big","summary":"The bounds crossed at left = 2, right = 1, and the code returns left = 2. But 2 * 2 = 4 overshoots 2: the floor root of 2 is 1, which is exactly where right landed.","codeLine":11,"state":{"nums":[0,1,2],"left":2,"right":1,"x":2,"labels":{"nums":"candidate"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let [left, right] = [0, x];\nwhile (left <= right) {\n    const mid = left + Math.floor((right - left) / 2);\n    const square = mid * mid;\n    if (square === x) {\n        return mid;\n    }\n    if (square < x) {\n        left = mid + 1;\n    } else {\n        right = mid - 1;\n    }\n}\nreturn left;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":14},"statementBundles":{"6":{"startLine":6,"endLine":7},"10":{"startLine":11,"endLine":13}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let [left, right] = [0, x];\nwhile (left <= right) {\n    const mid = left + Math.floor((right - left) / 2);\n    const square = mid * mid;\n    if (square === x) {\n        return mid;\n    }\n    if (square < x) {\n        left = mid + 1;\n    } else {\n        right = mid - 1;\n    }\n}\nreturn left;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":14},"statementBundles":{"6":{"startLine":6,"endLine":7},"10":{"startLine":11,"endLine":13}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int left = 0;\nint right = x;\nwhile (left <= right) {\n    int mid = left + Math.floorDiv((right - left), 2);\n    int square = mid * mid;\n    if (square == x) {\n        return mid;\n    }\n    if (square < x) {\n        left = mid + 1;\n    } else {\n        right = mid - 1;\n    }\n}\nreturn left;","lineMap":{"1":1,"2":3,"3":4,"4":5,"5":6,"6":7,"7":9,"8":10,"9":11,"10":12,"11":15},"statementBundles":{"1":{"startLine":1,"endLine":2},"6":{"startLine":7,"endLine":8},"10":{"startLine":12,"endLine":14}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"var (left, right) = (0, x);\nwhile (left <= right) {\n    int mid = left + (right - left) / 2;\n    int square = mid * mid;\n    if (square == x) {\n        return mid;\n    }\n    if (square < x) {\n        left = mid + 1;\n    } else {\n        right = mid - 1;\n    }\n}\nreturn left;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":14},"statementBundles":{"6":{"startLine":6,"endLine":7},"10":{"startLine":11,"endLine":13}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int left = 0;\nint right = x;\nwhile (left <= right) {\n    int mid = left + ((right - left) / 2);\n    int square = mid * mid;\n    if (square == x) {\n        return mid;\n    }\n    if (square < x) {\n        left = mid + 1;\n    } else {\n        right = mid - 1;\n    }\n}\nreturn left;","lineMap":{"1":1,"2":3,"3":4,"4":5,"5":6,"6":7,"7":9,"8":10,"9":11,"10":12,"11":15},"statementBundles":{"1":{"startLine":1,"endLine":2},"6":{"startLine":7,"endLine":8},"10":{"startLine":12,"endLine":14}}}}}},{"id":"edge-intro","chapter":"Edge cases","kind":"info","eyebrow":"Try to break it","title":"Which inputs could break this?","summary":"A floor-root search is only as good as the ends of its range and the arithmetic it runs there.","ctaLabel":"Check edge cases","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"edge-baseline","state":{"nums":[0,1,2,3,4],"left":0,"right":4,"x":4,"labels":{"nums":"candidate"}}}]},"body":["Two edges remain to pressure-test. At the smallest input, x = 0, the range collapses to a single candidate. At the largest, x near two billion, squaring a midpoint pushes against the 32-bit integer ceiling.","Run each one through and watch whether the bounds and the comparison hold up, or whether either edge needs special handling."]},{"id":"edge-zero","chapter":"Edge cases","kind":"case-probe","eyebrow":"Force a decision","title":"With x = 0, what does the search return?","summary":"x = 0, so left and right both start at 0.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"zero","state":{"nums":[0],"left":0,"right":0,"mid":0,"x":0,"labels":{"nums":"candidate"}}}]},"options":[{"id":"returns-zero","label":"0, found as an exact square","rationale":"left and right both start at 0, so mid is 0 and 0 * 0 equals x, which returns mid immediately. The single-candidate range needs no special handling.","isCorrect":true},{"id":"loops-forever","label":"It loops forever on candidate 0","rationale":"The window holds one candidate and the exact-square branch returns on the first probe, so there is no spin.","isCorrect":false},{"id":"needs-guard","label":"It needs a separate x = 0 guard before the loop","rationale":"Starting right at x means the range already includes 0 as a candidate, so the normal loop handles it without an extra branch.","isCorrect":false}],"answer":"0 falls out of the normal loop: both bounds start at 0, 0 * 0 equals 0, and the exact-square branch returns it on the first probe."},{"id":"edge-overflow","chapter":"Edge cases","kind":"case-probe","eyebrow":"Force a decision","title":"Why can mid * mid go wrong on a huge x?","summary":"x = 2,147,395,599, near the largest allowed value, with a midpoint candidate around 46,341.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"overflow","state":{"x":2147395599,"candidate":46341,"candidate squared":2147488281,"int32 max":2147483647}}]},"options":[{"id":"exceeds-32bit","label":"The product can exceed a 32-bit integer and wrap around","rationale":"A midpoint near 46,341 squares to roughly 2.1 billion, which overflows a signed 32-bit integer in fixed-width languages and corrupts the comparison. Computing the square in a wider type, or comparing mid against x / mid, keeps it safe.","isCorrect":true},{"id":"always-fine","label":"Nothing goes wrong, since x itself fits in 32 bits","rationale":"x fits, but mid * mid is a separate product that can be far larger than x before the comparison ever runs, so the multiplication itself is where the overflow happens.","isCorrect":false},{"id":"mid-overflows","label":"mid itself overflows during the search","rationale":"mid stays under sqrt(x), about 46,340, which is tiny. It is the square mid * mid, not mid, that can grow past the 32-bit limit.","isCorrect":false}],"answer":"mid stays small, but mid * mid can top two billion and overflow a 32-bit integer. Compute the square in a wider type, or compare mid with x / mid, so the test stays correct on the largest x."},{"id":"ready-to-code","chapter":"Code","kind":"info","eyebrow":"Get ready to code","title":"Turn the rule into code","summary":"When the window empties, every candidate through right fits and every candidate above it overshoots.","ctaLabel":"Start coding","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"ready","state":{"nums":[0,1,2,3,4,5,6,7,8],"left":0,"right":8,"x":8,"labels":{"nums":"candidate"}}}]},"body":["Set the window to the whole range 0 to x, probe the middle candidate, and compare its square to x with an overflow-safe test while the window is non-empty.","An exact square returns that candidate at once. An overshoot moves the upper bound below the midpoint; a fit moves the lower bound past it. When the bounds cross, return the upper bound, which is the largest candidate whose square does not exceed x."]},{"id":"coding","chapter":"Code","kind":"coding","source":"coding-checkpoints","eyebrow":"Code the rule","title":"Build the floor-root search","summary":"Initialize the search range, loop while it is non-empty, test the midpoint square, return exact hits, narrow the bounds, and return the last fitting candidate.","workspacePanel":"code","checkpoints":[{"id":"init-bounds","title":"Initialize search bounds","description":"Start the candidate root range at 0 through x.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"initializes_search_bounds","scope":"init-pointers","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Starting left at 1 and mishandling x = 0","Using an unrelated upper bound"],"hint":"Use left = 0 and right = x.","semanticSpec":{"intent":"Initialize the binary-search bounds over every possible integer root.","doneWhen":"The lower bound starts at 0 and the upper bound starts at x before the search loop.","shouldFailWhen":"This step should fail if either bound is missing, off by one, or unrelated to x.","roles":["left boundary","right boundary","candidate root range"],"mustProve":["The search includes 0 as a possible root.","The search includes x as the initial upper bound."],"mustNotAccept":["A range that starts at 1 and loses x = 0.","An upper bound unrelated to the input."],"proofSnippets":[{"id":"py-sqrtx-init-bounds","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x"}]}},{"id":"search-window","title":"Maintain the search window","description":"Continue while the candidate interval is non-empty, and stop as soon as the bounds cross.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"maintains_search_window","scope":"while-condition","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Using a loop that never narrows the range","Stopping before checking the final candidate"],"hint":"The termination condition follows the interval: loop while left <= right, then use the crossed-boundary state.","semanticSpec":{"intent":"Maintain an inclusive search window until the bounds cross.","doneWhen":"The loop condition is controlled by the active left and right search bounds.","shouldFailWhen":"This step should fail if the loop ignores either bound or stops before the final candidate.","roles":["search window","loop guard"],"mustProve":["The loop guard compares the active search bounds."],"mustNotAccept":["A fixed-count loop.","A loop controlled by a midpoint or square value instead of the bounds."],"proofSnippets":[{"id":"py-sqrtx-search-window","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            pass"}]}},{"id":"compute-mid","title":"Compute midpoint safely","description":"Derive mid from the current bounds each iteration.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"computes_midpoint_from_bounds","scope":"calculate-mid","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Using left + right / 2 without parentheses","Not recalculating mid inside the loop"],"hint":"Compute the midpoint from the current bounds in an overflow-safe way so each probe still splits the active interval.","semanticSpec":{"intent":"Compute a midpoint from the current search bounds.","doneWhen":"Each iteration derives mid from both active bounds.","shouldFailWhen":"This step should fail if mid is constant, outside the loop, or based on only one bound.","roles":["left boundary","right boundary","midpoint root candidate"],"mustProve":["The midpoint is computed inside the search loop.","The midpoint uses both bounds."],"mustNotAccept":["A fixed probe value.","Linear advancement without a midpoint."],"proofSnippets":[{"id":"py-sqrtx-compute-mid","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2"}]}},{"id":"compare-square","title":"Compare the midpoint safely","description":"Decide whether mid is exact, too small, or too large using a comparison that preserves the square-vs-x ordering without overflowing fixed-width integers.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"branches_on_midpoint_boundary_predicate","scope":"check-boundary-predicate","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Comparing mid directly to x","Using a square calculation that can overflow in fixed-width integer languages"],"hint":"Use any overflow-safe comparison that still separates the three cases: below x, equal to x, or above x.","semanticSpec":{"intent":"Branch on a midpoint-derived comparison against x.","doneWhen":"The code compares the midpoint square or equivalent quotient-safe value with x.","shouldFailWhen":"This step should fail if the branch is not tied to the midpoint candidate.","roles":["midpoint root candidate","square comparison","target input"],"mustProve":["The branch predicate depends on the midpoint.","The predicate compares the midpoint-derived value with x."],"mustNotAccept":["A branch comparing mid directly to x.","A branch unrelated to the midpoint probe."],"proofSnippets":[{"id":"py-sqrtx-compare-square","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                pass\n            if square < x:\n                pass"}]}},{"id":"return-exact-square","title":"Return exact square hits","description":"When mid squared equals x, return mid immediately.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"returns_search_hit","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Ignoring perfect squares","Returning square instead of mid"],"hint":"If square == x, return mid.","semanticSpec":{"intent":"Return the midpoint immediately when it is an exact square root.","doneWhen":"The exact-match branch returns the midpoint candidate.","shouldFailWhen":"This step should fail if the exact branch returns the square value or falls through.","roles":["exact-match branch","midpoint root candidate","return value"],"mustProve":["The return is guarded by the exact midpoint comparison.","The returned value is the midpoint candidate."],"mustNotAccept":["Returning square instead of mid.","Ignoring exact square matches."],"proofSnippets":[{"id":"py-sqrtx-return-exact-square","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid"}]}},{"id":"narrow-bounds","title":"Narrow around mid","description":"Update the active bounds according to the comparison result while preserving the last candidate that could still be the floor root.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"narrows_bounds_around_midpoint","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Forgetting the +1 or -1 update","Moving the wrong bound for square < x"],"hint":"On any non-perfect square, the too-small branch must keep the candidate recoverable and the too-large branch must exclude the rejected candidate.","semanticSpec":{"intent":"Narrow the bounds according to whether the midpoint is too small or too large.","doneWhen":"The too-small branch advances the lower bound past mid and the too-large branch lowers the upper bound below mid.","shouldFailWhen":"This step should fail if either bound moves the wrong direction or fails to exclude mid.","roles":["comparison branch","left boundary","right boundary","midpoint root candidate"],"mustProve":["The branch on the midpoint comparison controls both bound updates.","The lower bound advances after a too-small midpoint.","The upper bound retreats after a too-large midpoint."],"mustNotAccept":["Reversed bound updates.","Updates that can leave the same midpoint in the window."],"proofSnippets":[{"id":"py-sqrtx-narrow-bounds","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid\n            if square < x:\n                left = mid + 1\n            else:\n                right = mid - 1"}]}},{"id":"return-floor","title":"Return the floor root","description":"After the bounds cross on a non-perfect square, return the last candidate that was not too large.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"binary-search","fact":"returns_search_boundary","scope":"return-boundary","readMode":"binary-search-value-query-v1"}]},"commonMistakes":["Returning left after it has advanced past the answer","Returning -1 for non-perfect squares"],"hint":"Check an unseen non-square such as x = 15: after termination, right should be 3 because 4 has just been proven too large.","semanticSpec":{"intent":"Return the floor root from the crossed-boundary state.","doneWhen":"After the search loop exits, the code returns the boundary that still represents the last non-too-large candidate.","shouldFailWhen":"This step should fail if the code returns the advanced lower bound or a sentinel for non-perfect squares.","roles":["crossed search boundary","floor root candidate","return value"],"mustProve":["The terminal return occurs after the search loop.","The returned boundary is the floor-root side of the crossed bounds."],"mustNotAccept":["Returning left after it crossed past the floor.","Returning -1 for valid non-perfect squares."],"proofSnippets":[{"id":"py-sqrtx-return-floor","language":"python","code":"class Solution:\n    def mySqrt(self, x: int) -> int:\n        left, right = 0, x\n        while left <= right:\n            mid = left + (right - left) // 2\n            square = mid * mid\n            if square == x:\n                return mid\n            if square < x:\n                left = mid + 1\n            else:\n                right = mid - 1\n        return right"}]}}]},{"id":"complete","chapter":"Code","kind":"complete","eyebrow":"Take it with you","title":"What to carry forward","summary":"Binary search on the answer itself: the candidate roots are sorted by their squares, so each test halves the range.","workspacePanel":"code","body":["There is no array to search here. The sorted thing is the squares of the candidates, and the search runs over the candidate values directly, asking each midpoint whether its square clears x.","Correctness rests on one move: a candidate that fits stays a possible answer, so on a miss the answer is right, the last candidate that fit, never left, which has stepped onto an overshoot."],"takeaways":[{"title":"The window holds only candidates that could still be the floor","body":"left and right bound the roots not yet ruled out; everything below left is proven to fit and everything above right is proven to overshoot."},{"title":"On a miss, return right","body":"When the bounds cross, right rests on the largest candidate whose square stayed under x, while left has advanced onto one that overshot."},{"title":"O(log x), and watch the square","body":"Each test discards half the candidates, so a range of two billion is settled in about 31 probes; compute mid * mid in a wide enough type so it cannot overflow on the largest x."}],"nextAction":"Submit when you are ready. TraceCode will run the final checks against the code you wrote."}],"slidesOrigin":"authored"}}
