Magicsheet logo

Check Balanced String

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Topics

Check Balanced String

What is this problem about?

The "Check Balanced String interview question" is a basic string manipulation and arithmetic challenge. You are given a numeric string, and you need to determine if it is "balanced." A string is balanced if the sum of digits at even indices is equal to the sum of digits at odd indices. This problem tests your ability to iterate through a string and perform selective aggregation.

Why is this asked in interviews?

Amazon and other tech companies use the "Check Balanced String coding problem" as a warm-up exercise. it evaluates a candidate's basic programming fluency, specifically their ability to handle strings, convert characters to integers, and manage loops with index-based logic. It’s a classic entry-level "String interview pattern" question.

Algorithmic pattern used

This problem follows the Linear Scan and Parity Check pattern.

  1. Iterate: Use a single loop to move through the string from index 00 to n1n-1.
  2. Summation: Maintain two variables, even_sum and odd_sum.
  3. Condition: For each index i, add the numeric value of the character to the appropriate sum based on whether i % 2 is 0 or 1.
  4. Comparison: Return true if the two sums are equal.

Example explanation

String: "1234"

  • Even index 0: '1'. even_sum = 1.
  • Odd index 1: '2'. odd_sum = 2.
  • Even index 2: '3'. even_sum = 1 + 3 = 4.
  • Odd index 3: '4'. odd_sum = 2 + 4 = 6. Compare: 4eq64 eq 6. Result: False. String: "1212"
  • even_sum = 1 + 1 = 2.
  • odd_sum = 2 + 2 = 4. Compare: 2eq42 eq 4. Result: False. Wait, let's try "1322":
  • even_sum = 1 + 2 = 3.
  • odd_sum = 3 + 2 = 5. Wait, "1212" was 1+1=21+1=2 vs 2+2=42+2=4. How about "4545"?
  • even_sum = 4 + 4 = 8.
  • odd_sum = 5 + 5 = 10. Balanced example: "1232"
  • even_sum = 1 + 3 = 4.
  • odd_sum = 2 + 2 = 4. Result: True.

Common mistakes candidates make

  • Character to Integer Conversion: Forgetting that '1' is not the integer 1. You must subtract '0' or use a parsing function.
  • Index Parity: Getting confused about whether index 0 is considered even or odd. Standard convention is that index 0 is even.
  • Off-by-one: Failing to process the last character of the string.

Interview preparation tip

Practice working with strings as arrays of characters. Be comfortable with index-based loops and basic parity math (i % 2). These are the building blocks for more complex "String interview pattern" problems.

Similar Questions