Check If a String Contains All Binary Codes of Size K
MediumStringshashing
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given a binary string s (only '0' and '1') and an integer k.
Return true if every one of the 2^k binary strings of length k appears somewhere in s as a contiguous substring, and false otherwise.
Aim for O(n) time (treating k as small) by avoiding repeated substring copies.
Examples
Input: s = "00110110", k = 2
Output: true
The substrings 00, 01, 11, and 10 all appear.
Input: s = "0110", k = 1
Output: true
Both 0 and 1 appear.
Input: s = "0110", k = 2
Output: false
00 never appears.
Constraints
- 1 <= s.length <= 5 * 10^5
- s[i] is '0' or '1'
- 1 <= k <= 20
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.