Valid Binary Prefix
Updated
Problem statement
Given a binary sequence of length n as a string, perform the following operation any number of times:
- Append either a
'0'or a'1'to the end of the sequence.
A sequence is considered valid if it is possible to make the total number of "10" subsequences in the updated sequence exactly equal to k.
Your task is to count the total number of valid non-empty prefix sequences of the given binary sequence.
Notes:
- A sequence is a subsequence if it can be obtained by deleting digits, possibly none, without altering the relative positions.
- A non-empty prefix sequence is any sequence derived by deleting digits from the end of the given sequence, ensuring the length of the prefix is at least
1.
Function
countValidBinaryPrefixes(sequence: String, k: int) → intExamples
Example 1
sequence = "100"k = 1return = 2Analyze all non-empty prefix sequences:
| Prefix Sequence | Appended String | Sequence Formed After Append | Is Good? |
|---|---|---|---|
"1" | "01" | "101" | Good (There are k "10" sequences) |
"10" | Blank | "10" | Good (There are k "10" sequences) |
"100" | -- | -- | Not Good (Not possible) |
For "100", there are already 2 subsequences of "10": indices (0, 1) and indices (0, 2). Hence, the number of non-empty prefix sequences of the given binary sequence is 2.
Return 2.
More Goldman Sachs Online Assessment questions
- Maximum Ones After K OperationsOA · Seen Nov 2026
- Palindrome CounterOA · Seen Oct 2026
- Longest Palindromic SubsequenceOA · Seen Oct 2026
- Count Redundant File BackupsOA · Seen Oct 2026
- Minimum Distinct Prefix CostOA · Seen Oct 2026
- Merge Overlapping IntervalsOA · Seen Sep 2026
- Data ReorganizationOA · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsOA · Seen Jul 2026
- Find the Winner of an Array GameOA · Seen Jun 2026
Similar questions at other companies
- Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
- Merging PalindromesAtlassian · OAAtlassian interview questions
- Merging PalindromesZscaler · PHONE SCREENZscaler interview questions
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
- Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions