Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
Given the integer n, return the number of complete rows of the staircase you will build.
Example 1:
Input: n = 5 Output: 2 Explanation: Because the 3rd row is incomplete, we return 2.
Example 2:
Input: n = 8 Output: 3 Explanation: Because the 4th row is incomplete, we return 3.
Constraints:
1 <= n <= 231 - 1Problem summary: You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. Given the integer n, return the number of complete rows of the staircase you will build.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Binary Search
5
8
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #441: Arranging Coins
class Solution {
public int arrangeCoins(int n) {
return (int) (Math.sqrt(2) * Math.sqrt(n + 0.125) - 0.5);
}
}
// Accepted solution for LeetCode #441: Arranging Coins
func arrangeCoins(n int) int {
left, right := 1, n
for left < right {
mid := (left + right + 1) >> 1
if (1+mid)*mid/2 <= n {
left = mid
} else {
right = mid - 1
}
}
return left
}
# Accepted solution for LeetCode #441: Arranging Coins
class Solution:
def arrangeCoins(self, n: int) -> int:
return int(math.sqrt(2) * math.sqrt(n + 0.125) - 0.5)
// Accepted solution for LeetCode #441: Arranging Coins
impl Solution {
pub fn arrange_coins(n: i32) -> i32 {
let (mut l, mut r) = (1, n);
let mut res = 0;
while l <= r {
let mid = l + (r - l) / 2;
let coins = (mid as i64 * (mid as i64 + 1)) / 2;
if coins > n as i64 {
r = mid - 1;
} else {
l = mid + 1;
res = mid;
}
}
res as i32
}
}
// Accepted solution for LeetCode #441: Arranging Coins
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #441: Arranging Coins
// class Solution {
// public int arrangeCoins(int n) {
// return (int) (Math.sqrt(2) * Math.sqrt(n + 0.125) - 0.5);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.