Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.
Example 1:
Input: num = "51230100" Output: "512301" Explanation: Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301".
Example 2:
Input: num = "123" Output: "123" Explanation: Integer "123" has no trailing zeros, we return integer "123".
Constraints:
1 <= num.length <= 1000num consists of only digits.num doesn't have any leading zeros.Problem summary: Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"51230100"
"123"
check-if-bitwise-or-has-trailing-zeros)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2710: Remove Trailing Zeros From a String
class Solution {
public String removeTrailingZeros(String num) {
int i = num.length() - 1;
while (num.charAt(i) == '0') {
--i;
}
return num.substring(0, i + 1);
}
}
// Accepted solution for LeetCode #2710: Remove Trailing Zeros From a String
func removeTrailingZeros(num string) string {
i := len(num) - 1
for num[i] == '0' {
i--
}
return num[:i+1]
}
# Accepted solution for LeetCode #2710: Remove Trailing Zeros From a String
class Solution:
def removeTrailingZeros(self, num: str) -> str:
return num.rstrip("0")
// Accepted solution for LeetCode #2710: Remove Trailing Zeros From a String
impl Solution {
pub fn remove_trailing_zeros(num: String) -> String {
let mut i = num.len() - 1;
while num.chars().nth(i) == Some('0') {
i -= 1;
}
num[..i + 1].to_string()
}
}
// Accepted solution for LeetCode #2710: Remove Trailing Zeros From a String
function removeTrailingZeros(num: string): string {
let i = num.length - 1;
while (num[i] === '0') {
--i;
}
return num.substring(0, i + 1);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.