PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of recursive grammars and string parsing, focusing on designing linear-time algorithms and managing space constraints for validity checks on binary strings.

  • nan
  • Voleon
  • Coding & Algorithms
  • Software Engineer

Validate whether a binary string is good

Company: Voleon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: nan

Interview Round: Technical Screen

You are given a binary string `s` consisting only of characters `'0'` and `'1'`. Define a **good string** recursively by the grammar: - `'0'` is a good string. - If `a` and `b` are good strings, then `'1' + a + b` (concatenation) is also a good string. Examples of good strings: - `"0"` - `"100"` (=`1` + `0` + `0`) - `"11000"` (=`1` + `100` + `0`) - `"1100100"` (=`1` + `100` + `100`) ### Task Return whether `s` is a good string. ### Requirements - Target time complexity: **O(n)** - Use **O(1)** or **O(n)** extra space. ### Input/Output - Input: string `s` - Output: boolean (`true` if good, else `false`) ### Constraints (reasonable interview constraints) - `1 <= len(s) <= 10^6`

Quick Answer: This question evaluates understanding of recursive grammars and string parsing, focusing on designing linear-time algorithms and managing space constraints for validity checks on binary strings.

You are given a binary string s consisting only of characters '0' and '1'. A string is called good if it can be generated by the following recursive grammar: '0' is good; if a and b are good strings, then '1' + a + b is also good. Return True if s is a good string, otherwise return False.

Constraints

  • 1 <= len(s) <= 10^6
  • s[i] is either '0' or '1'
  • Target time complexity: O(n)
  • Extra space: O(1) or O(n)

Examples

Input: ('0',)

Expected Output: True

Explanation: The single string '0' is directly defined as good.

Input: ('1',)

Expected Output: False

Explanation: A '1' must be followed by two good strings, but none are present.

Input: ('100',)

Expected Output: True

Explanation: '100' equals '1' + '0' + '0', so it is good.

Input: ('11000',)

Expected Output: True

Explanation: '11000' equals '1' + '100' + '0', and both parts are good.

Input: ('1100100',)

Expected Output: True

Explanation: '1100100' equals '1' + '100' + '100', where both '100' substrings are good.

Input: ('10',)

Expected Output: False

Explanation: The leading '1' creates two required child strings, but only one '0' is available.

Input: ('000',)

Expected Output: False

Explanation: The first '0' already completes a full good string, so the remaining characters cannot be part of the same valid string.

Input: ('1110000',)

Expected Output: True

Explanation: This is a valid skewed structure: each '1' introduces two required children, and the four '0' leaves satisfy them exactly.

Solution

def solution(s):
    slots = 1

    for ch in s:
        slots -= 1
        if slots < 0:
            return False

        if ch == '1':
            slots += 2
        elif ch != '0':
            return False

    return slots == 0

Time complexity: O(n). Space complexity: O(1).

Hints

  1. Think of '1' as an internal node that must have two child good strings, and '0' as a leaf.
  2. Track how many unresolved child positions are currently required while scanning from left to right.
Last updated: Jun 14, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve classic coding interview problems - Voleon (hard)
  • Count White Balls After K Steps - Voleon (hard)
  • Implement sparse matrix addition and multiplication - Voleon (nan)
  • Count queen attacks on points with blockers - Voleon (nan)
  • Simulate an exchange and participation-rate trading - Voleon (nan)