Validate whether a binary string is good
Company: Voleon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
Read the full Voleon Software Engineer interview experience this question came from
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.
Hints
- Think of '1' as an internal node that must have two child good strings, and '0' as a leaf.
- Track how many unresolved child positions are currently required while scanning from left to right.