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.
You are given a string num consisting of only digits. A string of digits is called balanced if the sum of the digits at even indices is equal to the sum of digits at odd indices.
Return true if num is balanced, otherwise return false.
Example 1:
Input: num = "1234"
Output: false
Explanation:
1 + 3 == 4, and the sum of digits at odd indices is 2 + 4 == 6.num is not balanced.Example 2:
Input: num = "24123"
Output: true
Explanation:
2 + 1 + 3 == 6, and the sum of digits at odd indices is 4 + 2 == 6.num is balanced.Constraints:
2 <= num.length <= 100num consists of digits onlyProblem summary: You are given a string num consisting of only digits. A string of digits is called balanced if the sum of the digits at even indices is equal to the sum of digits at odd indices. Return true if num is balanced, otherwise return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"1234"
"24123"
balanced-binary-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3340: Check Balanced String
class Solution {
public boolean isBalanced(String num) {
int[] f = new int[2];
for (int i = 0; i < num.length(); ++i) {
f[i & 1] += num.charAt(i) - '0';
}
return f[0] == f[1];
}
}
// Accepted solution for LeetCode #3340: Check Balanced String
func isBalanced(num string) bool {
f := [2]int{}
for i, c := range num {
f[i&1] += int(c - '0')
}
return f[0] == f[1]
}
# Accepted solution for LeetCode #3340: Check Balanced String
class Solution:
def isBalanced(self, num: str) -> bool:
f = [0, 0]
for i, x in enumerate(map(int, num)):
f[i & 1] += x
return f[0] == f[1]
// Accepted solution for LeetCode #3340: Check Balanced String
impl Solution {
pub fn is_balanced(num: String) -> bool {
let mut f = [0; 2];
for (i, x) in num.as_bytes().iter().enumerate() {
f[i & 1] += (x - b'0') as i32;
}
f[0] == f[1]
}
}
// Accepted solution for LeetCode #3340: Check Balanced String
function isBalanced(num: string): boolean {
const f = [0, 0];
for (let i = 0; i < num.length; ++i) {
f[i & 1] += +num[i];
}
return f[0] === f[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.