Magicsheet logo

Check if Number Has Equal Digit Count and Digit Value

Easy
70.4%
Updated 6/1/2025

Check if Number Has Equal Digit Count and Digit Value

What is this problem about?

The "Check if Number Has Equal Digit Count and Digit Value interview question" is a self-referential string challenge. You are given a numeric string num. You need to determine if, for every index i in the string, the digit at that index is equal to the number of times the digit i appears in the entire string.

Why is this asked in interviews?

Companies like Meta and Google ask the "Check if Number Has Equal Digit Count coding problem" to test a candidate's ability to implement simple but precise validation logic. It evaluates "Hash Table interview pattern" skills (frequency counting) and the ability to map indices to values.

Algorithmic pattern used

This problem follows the Frequency Counting and Linear Comparison pattern.

  1. Count Frequencies: Iterate through the string and store the count of each digit ('0' to '9') in a frequency map or an array of size 10.
  2. Verification: Iterate through the string again. For each index i:
    • Get the digit at that index (let's call it count_from_string).
    • Get the actual count of index i from your frequency map (let's call it actual_count).
    • If actual_count != count_from_string, return false.
  3. Success: If the loop finishes, return true.

Example explanation

String: "1210"

  • Index 0: value '1'. Does '0' appear 1 time? Yes.
  • Index 1: value '2'. Does '1' appear 2 times? Yes.
  • Index 2: value '1'. Does '2' appear 1 time? Yes.
  • Index 3: value '0'. Does '3' appear 0 times? Yes. Result: True. String: "030"
  • Index 0: value '0'. Does '0' appear 0 times? No, it appears 2 times. Result: False.

Common mistakes candidates make

  • Character/Integer mix-up: Forgetting to convert the characters in the string to their numeric values before comparison.
  • Double-counting: Trying to do the check in a single pass before the frequency map is fully built.
  • Index boundaries: Not realizing that the string index refers to the digit value being counted.

Interview preparation tip

For problems that involve counting digits within a string, a fixed-size array of length 10 is almost always more efficient than a full Hash Map. This is a standard "String interview pattern" optimization.

Similar Questions