def count_valid_prefixes(sequence: str, k: int) -> int:
Given a binary string sequence and an integer k, you may append any number of characters, each '0' or '1', to the end of a string. A string counts as valid if, after appending, the number of "10" subsequences in it is exactly k. (A "10" subsequence is any pair of positions i < j with a '1' at i and a '0' at j.)
Return how many non-empty prefixes of sequence are valid.
sequence = "11", k = 1 -> 1
sequence = "101", k = 2 -> 2
In the second example, "1" and "10" are valid, but "101" is not: it already has one pair, and appending a '0' adds two more, jumping past 2.
k were reported, and no function name. One candidate's solution recounted every prefix (O(n²)) and passed every test.Three reports from 2025 campus and intern online assessments: two problems in 75 minutes, with this one first. One candidate posted two sample cases; another called the time generous. A third wrote up the problem with an O(n^2) solution that passed every test, took the assessment on the last day of the window, and was still rejected, so passing the tests is not the whole story.
Given a binary string sequence and an integer k, you may append any number of characters, each '0' or '1', to the end of a string. A string counts as valid if, after appending, the number of "10" subsequences in it is exactly k. (A "10" subsequence is any pair of positions i < j with a '1' at i and a '0' at j.)
Return how many non-empty prefixes of sequence are valid.
sequence = "11", k = 1 -> 1
sequence = "101", k = 2 -> 2
In the second example, "1" and "10" are valid, but "101" is not: it already has one pair, and appending a '0' adds two more, jumping past 2.
k were reported, and no function name. One candidate's solution recounted every prefix (O(n²)) and passed every test.What passers do: One candidate recounted the pairs and 1s for every prefix, then checked pairs == k or k - pairs >= number of 1s; it passed every test case
Why people fail: That same candidate passed every test case, took the assessment on the last day of the window, and was still rejected
Send them this page. It is free to read, no account needed.
What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 62 Citadel questions, refreshed monthly from new candidate reports.